Reputation: 3
I'm setting up my .htaccess files with some RewriteRules, but unfortunatly I got stuck. I've found a lot of topics on this, but none mentioning my specific issue. I have this:
RewriteEngine On
RewriteBase /
RewriteRule ^en/contact /contact.php?lang=en&slug=ok [L,NC]
RewriteRule ^contact /en/contact [L,R=301]
Purpose is to force the lang as a first parameter in the url if not set. So: www.example.com/contact should be redirected (301) to www.example.com/en/contact which then again loads the content from /contact.php?lang=en&slug=ok
The problem I have is that I end up in a loop as /contact.php?lang=en&slug=ok gets loaded and is parsed by RewriteRule ^contact /en/contact [L,R=301] again.
How can I adapt this rule so it is only triggered for /contact and not for /contact.php or /en/contact or /contactme or any other url that contains contact
Thanks!
Upvotes: 0
Views: 1684
Reputation: 41229
Your 301 redirect rule is causing the infinite loop as it matches the destination path of your first rule. What is happening is that when you request /en/contact
your first rule rewrites it to /contact.php?lang=en&slug=ok
and then ,your second rule matches the same uri and redirects it to /en/contact
.
To fix this, You can either use END flag in your RewriteRule or use THE_REQUEST
condition .
RewriteEngine On
RewriteBase /
RewriteRule ^en/contact /contact.php?lang=en&slug=ok [L,NC,END]
RewriteRule ^contact /en/contact [L,R=301]
Note : END
flag works only on apache versions above 2.4
if your server version is less then 2.4 you could use a % {THE_REQUEST}
condition to terminate the redirect loop
RewriteEngine On
RewriteBase /
RewriteRule ^en/contact /contact.php?lang=en&slug=ok [L,NC]
RewriteCond %{THE_REQUEST} /contact
RewriteRule ^contact /en/contact [L,R=301]
Make sure to clear your browser cache before testing these redirects.
Upvotes: 0