When I enter my example.com
url I want to redirect to example.com/en/
but when I enter example.com/demo
I want to redirect to example.com/us/demo
. The word demo here is just variable. how can I do that ?
My .htaccess code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /tr/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /tr/index.php [L]
</IfModule>
CodePudding user response:
Try the following before your existing directives (ie. before the <IfModule>
section):
# Redirect "/" to "/en/"
RewriteRule ^$ /en/ [R,L]
# Redirect "/demo" to "/us/demo"
RewriteCond %{REQUEST_URI} !^/(en|us)/
RewriteRule (.*) /us/$1 [R,L]
The first rule redirects example.com/
to example.comn/en/
. The second rule redirects everything else to /us/<url>
, providing the URL-path does not already start /en/
or /us/
.
If you also need to make an exception for your static assets... eg. Should a request for /assets/images/img.png
also be redirected? Then include an additional condition (as the 2nd condition) on the second rule:
# Redirect "/demo" to "/us/demo" (and exclude static assets)
RewriteCond %{REQUEST_URI} !^/(en|us)/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) /us/$1 [R,L]
Note that these are 302 (temporary) redirects.