Turns out after researching, one site found duplicate content on my website.

The reason is because the unfriendly php file that friendly URLs link to was apparently discovered by a robot and then got spread.

What I want to do is fix this issue without causing a redirect loop.

I'll explain by example:

Currently To redirect the contacts page with mod_rewrite, I use:

RewriteRule ^contact$ /index2.php?A=CONTACT [L]

But the problem is http://example.com/contact and http://example.com/index2.php?A=CONTACT are both accessible and point to exactly the same page, yet I don't want the latter URL directly accessible in a browser.

I was thinking using something like:

RewriteRule ^index2.php$ ??? [L]
RewriteRule ^contact$ /index2.php?A=CONTACT [L]

Because I need to handle the php file but I can't replace the ??? with /contact or I'll be creating an endless loop.

I was thinking of adding this to the top to prevent a second round of looping from happening:

RewriteCond %{ENV:REDIRECT_STATUS} !^$
RewriteRule .* - [L]

But I'm not sure if that's of any help.

If anyone has run into this situation can someone give me a hand on how to fix it?

  • Not sure you got my last ping... I updated the answer with the assumption there was a /contact directory. If not, then it is back to the drawing board.
    – closetnoc
    Jul 3, 2015 at 17:52

1 Answer 1


In order to redirect from the "unfriendly" URL to the "friendly" URL, avoiding rewrite loops, you can check against %{THE_REQUEST} (which contains the initial HTTP request). For example:

RewriteRule ^index2\.php /contact? [R=301,L]

This should come before the internal rewrite that turns the "friendly" URL into the actual URL.

Since you are using different case for the URL parameter ("CONTACT") and the URL-path ("contact") it will be tricky to make this rule more generic without using RewriteMap in your server config (not .htaccess). Although this could be handled in your code instead.

  • @MIke Try this first!!
    – closetnoc
    Jul 3, 2015 at 22:39
  • w3d's answer seems to help with a question mark added before [R=301,L] What I did in my php code however was check the redirect_status variable since it returns a number on any RewriteRule processed. If w3d's idea lowers the overall TTFB of the site then I'll use it instead. Jul 4, 2015 at 0:26
  • Ah yes (oops forgot!), the ? is required at the end of the RewriteRule substitution in order to remove the query string (added to answer). (Or you can use the QSD flag if using Apache 2.4+). Doing this in Apache config (.htaccess) is going to be quicker and more efficient than handing this over to PHP. You can check %{ENV:REDIRECT_STATUS} in .htaccess as well (it is an Apache environment variable), but it is no better than the above.
    – MrWhite
    Jul 4, 2015 at 0:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.