: So I wrote a set of Rewrite rules that did what you wanted, but it completely broke my website. I realized that what you want is probably not what you need. Adding trailing slashes to the
So I wrote a set of Rewrite rules that did what you wanted, but it completely broke my website. I realized that what you want is probably not what you need. Adding trailing slashes to the end of all URLs really messes with the semantics of the URL in that you're no longer accessing the file /foo but the content listing of the directory /foo/.
For example:
changing /mypage to /mypage/ will probably break any relative links. If you reference a Javascript file <script src="myscript.js">, instead of looking for /myscript.js, the browser will look for /mypage/myscript.js. You would need to change your source to read <script src="../myscript.js"> which 1) doesn't make sense to the author, and 2) looks uglier than not having trailing slashes.
For reference:
RewriteCond %{REQUEST_FILE}.html -f
RewriteRule (.*)$ .html [L]
RewriteCond %{REQUEST_FILE}.php -f
RewriteRule (.*)$ .php [L]
RewriteCond %{REQUEST_FILE}.cgi -f
RewriteRule (.*)$ .cgi [L]
would change only php, cgi, and html extensions, but a better idea would be to use Apache2 content negotiation (with MultiViews).
Edit:
The original code. Or at least part of it. I broke it, and then cut it down to the above, and now I can't quite remember what I did. But it does everything except remove trailing extensions.
# This block adds the trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond /your/web/directory%{REQUEST_URI}.html -f [OR]
RewriteCond /your/web/directory%{REQUEST_URI}.php -f [OR]
RewriteCond /your/web/directory%{REQUEST_URI}.cgi -f
RewriteRule .* %{REQUEST_URI}/ [R=301,L]
# These blocks redirect /foo/ to /foo.html and so on
RewriteCond %{REQUEST_URI} (.*)/$
RewriteCond /your/web/directory%1.html -f
RewriteRule (.*)/$ .html [L]
RewriteCond %{REQUEST_URI} (.*)/$
RewriteCond /your/web/directory%1.php -f
RewriteRule (.*)/$ .php [L]
RewriteCond %{REQUEST_URI} (.*)/$
RewriteCond /your/web/directory%1.cgi -f
RewriteRule (.*)/$ .cgi [L]
You can email me at mazin (at) aztekera.com if you'd like.
More posts by @LarsenBagley505
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2025 All Rights reserved.