Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

The requirements:

  • Work inside .htaccess (preferably via mod_rewrite)
  • Redirect requests from /any/directory/index.php to /any/directory/
  • Make sure that /index.php-with-stupid-stuff-here does not get rewritten
  • Add trailing slash to directories, /another/directory becomes /another/directory/

Also note that:

  • The ideal scenario would be to use manual 301 redirects, but this is a case where there are far too many directories to do that for individually.
  • Internal linking is clean (always goes to the right place), so this is a just-in-case solution. (But others who find this question might be in a different situation.)

DaveRandom helped me revise my solution, resulting in…

RewriteEngine On
RewriteCond %{THE_REQUEST} (?:/[^/]*)*/index\.php[?#\ ]
RewriteRule .* %{REQUEST_URI}/../ [L,R=301]

The basic idea behind it is that by using %{THE_REQUEST} in a RewriteCond you are looking at the actual URI requested by the user before any file system mappings, alias, other rewrite rules etc. were applied.

This is important, because if you just look at the request-URI in a RewriteRule you will almost definitely end up in a redirect loop.

Questions:

  • Is that solid, correct code?
  • Anything else that should be added, or taken into account?
  • Or, considering best practices, is there a better way entirely?
share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.