Search code examples
php.htaccess

Request URI Match and Redirect to Specific Link htaccess


I'm trying to redirect all root users to a specific link eg: example.com to example.com/en1 which an index.php file, so if any user type example.com/en2 or en3 or en100 will redirect to example.com/en1

I have tried this code

if (strpos($_SERVER['REQUEST_URI'], "en1") == false){
    header('Location: en1');
}

But the problem is, when I type en2 or en100 it shows 404 error not redirecting to en1

Also tried htaccess

RedirectMatch ^/$ /en1
RewriteRule en1 index.php

But no luck!

How do i fix it?


Solution

  • You can use RewriteRules like this :

    RewriteEngine On
    #redirect / to /en1
    RewriteRule ^$ https://example.com/en1 [R,L]
    #redirect /en123 to /en1
    RewriteCond %{REQUEST_URI} !/en1$
    RewriteRule ^en[0-9]+ https://example.com/en1 [L,R]
    

    Let me know how it works for you.