How can I add a trailing slash to:
RewriteEngine on
RewriteRule !\.(js|ico|gif|jpg|png|css|html)$ index.php
I have tried with:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+[^/])$ $1/ [R]
But it’s not working
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
See the detail/description line for the CondPatterns -d and -f for RewriteCond. See how it says “tests whether or not it exists, and is a regular file”? That is expensive and does not scale well at all. Do you really care if it’s “a regular file”? No, you only care if it looks like a file. (matches the filename.extension pattern) So, I’m going to improve your Rewrite while I’m at it.
See the detail/description for the ‘last|L’ flag for RewriteRule. See how it says “don’t apply any more rewrite rules”? I bet that’s what you want isn’t it? You want either index.php to be used when a request doesn’t match the extension of a static asset, OR you want to put a slash on the end of a request that doesn’t match the filename.extension pattern. Not both.
Here’s how you do that:
This is a very good answer considering that you didn’t tell us want you are trying to do. If you want a better answer, you’ll need to give us more info than “I copied and pasted 2 code snipets together, but it’s not working.”
Update: I hastily threw together an edit to this answer based on cornegigouille’s answer. Here is a bash shell transcript of the proof I did to test it. Let me know if you see a bug.