Mobile app version of vmapp.org
Login or Join
Harper822

: Stopping accessibility of internal script files via mod_rewrite Turns out after researching, one site found duplicate content on my website. The reason is because the unfriendly php file that friendly

@Harper822

Posted in: #ModRewrite #Script #UrlRewriting

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 example.com/contact and 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?

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Harper822

1 Comments

Sorted by latest first Latest Oldest Best

 

@Jamie184

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:

RewriteCond %{THE_REQUEST} ?A=CONTACT
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.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme