: .htaccess redirect for one file Somehow an erroneous URL of mine got indexed by Google. Which is: www.example.com/index.php/ All I want to do is redirect it to the same URL without the /
Somehow an erroneous URL of mine got indexed by Google. Which is:
example.com/index.php/
All I want to do is redirect it to the same URL without the / at the end. I have the below that seems to work well. I just want to double check this will only affect example.com/index.php/ and nothing else such as an index.php in a subfolder or the like.
Redirect 301 /index.php/ /index.php
More posts by @Alves908
1 Comments
Sorted by latest first Latest Oldest Best
Redirect 301 /index.php/ /index.php
This works OK-ish. It won't affect any other index.php in a subdirectory. However, it will redirect more than just /index.php/. The Redirect directive (part of mod_alias) is prefix matching, so the above Redirect will also match /index.php/<something>. And everything after the match, ie. <something>, is copied onto the end of the target URL. However, since you have omitted the trailing slash from the target URL, this will become /index.php<something>, which will likely result in a 404.
The fact that /index.php/<something> would result in a 404 (as opposed to a valid request) is probably a good thing, since you don't want to run the risk of /index.php/<something> (or the target) being indexed. However, you could change this so that /index.php<something> (where <something> is / or /<anything>) simply redirects to /index.php by using the RedirectMatch directive instead:
RedirectMatch 301 ^/index.php. /index.php
The RedirectMatch directive takes a regex. The trailing unescaped . (dot) on the pattern matches any character.
If you wanted to match only /index.php/ (exactly) then change this to:
RedirectMatch 301 ^/index.php/$ /index.php
NB: This is assuming you are not already using mod_rewrite (ie. RewriteRule) for other redirects/rewrites. It is advisable not to mix redirects from both modules, to avoid potential conflicts.
The / (or /something) on the end of a valid filename is called additional pathname information (aka path info / PATH_INFO).
If in the future, you wanted to block all requests that contain path info then you can add the following directive to your server config (or .htaccess file):
AcceptPathInfo Off
This will result in all requests that contain path info resulting in a 404.
However, bear in mind that some CMS use path info for routing the URL.
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.