Somehow an erroneous URL of mine got indexed by Google. Which is:


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 www.example.com/index.php/ and nothing else such as an index.php in a subfolder or the like.

Redirect 301 /index.php/ /index.php

1 Answer 1

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.

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.