Is blog a directory? If so, Apache will do that for you while being annoyed that someone would not provide the correct URI for the directory ... AND specify the file in that directory (except via DirectoryIndex). IMHO, poor technique for a webmaster.

If not, are you using MultiViews?

Please explain your intent to make it easier to provide help.

Regards,

DK

champ
—
2012-06-01T06:30:58Z —
#3

dklynn said:

Is blog a directory? If so, Apache will do that for you while being annoyed that someone would not provide the correct URI for the directory ... AND specify the file in that directory (except via DirectoryIndex). IMHO, poor technique for a webmaster.

If not, are you using MultiViews?

Please explain your intent to make it easier to provide help.

Thanks for responding DK. blog is directory. MultiViews is disabled. I know how to redirect a directory from no-slash to slash. But I'm not sure how to have the add trailing slash mod_rewrite code ignore url's containing a question mark. If it's a question mark it's a search query and I wouldn't want a forward slash added to the end. So basically, add a trailing slash to every URL that's 1) not a query url and 2) is missing a trailing slash.

Thanks.

ServerStorm
—
2012-06-01T20:11:37Z —
#4

Hi Champ,

I am just learning this so it may not make sense, but will this work for you? It worked when I test it.

Unfortunately I did this post from my phone and didn't have a way to test, but does this get it done?

Regards,Steve

ScallioXTX
—
2012-06-01T21:05:11Z —
#7

Not quite

RewriteEngine On
# Check if query string exists
RewriteCond %{QUERY_STRING} ^$
<font color='"#FF0000"'># Check that the request is not for an existing file
RewriteCond %{REQUEST_FILENAME} !-f</font>
# When REQUEST_URI does not have a trailing slash
RewriteCond %{REQUEST_URI} !/$
#Does the 301 redirect to the url with the appended trailing slash (replace your domain).
RewriteRule .? http://your_domain.com%{REQUEST_URI}/ [NC,L,R=301]

I've removed the RewriteBase -- not really needed

The [OR] on the %{QUERY_STRING} wasn't correct; all conditions need to hold for the RewriteRule to fire

Use %{REQUEST_FILENAME} !-f to check that the request is not for an existing file

If you're not interested in part of the URL, don't match it and don't create a backreference. i.e., instead of RewriteCond %{REQUEST_URI} !(.*)/$ just use RewriteCond %{REQUEST_URI} !/$ -- does exactly the same but is lighter since it only has to check the last character of the string

Instead of (.*) ... $1 use .? ... %{REQUEST_URI} -- faster as it doesn't create new variables but just uses variables already available in mod_rewrite. Again, if you don't really care about it, don't match it

But, really not bad for someone who's learning. I've seen a LOT worse

ServerStorm
—
2012-06-01T21:18:29Z —
#8

Wow! Thanks ScallioXTX!

Solved it for the OP, and taught me at the same time. Way to go.

I understand your recommendations and will try to think in this 'lets use "less hungry" regex to perform similar tasks to their greety cousins.

Regards,Steve

dklynn
—
2012-06-03T09:15:37Z —
#9

Rémon,

Aw, you know to NEVER use the No Case flag in a RewriteRule because Apache is case sensitive. It won't make any difference to your code, however, as and your .? already matched ANYTHING.

Of course, I MUST reiterate my constant admonition NOT to use a trailing / (except on a directory (-d)) as that will tell visitor browsers to look for relatively linked files in the wrong place (generating a lot of 404's and broken web pages).

Others,

No Case is to be used with strings like {HTTP_HOST} which are case insensitive. Please reserve NC to those strings so you aren't hit with unanticipated problems.