Forum Moderators: phranque

Message Too Old, No Replies

Trailing slash problem

         

Karma

8:36 pm on Jul 21, 2011 (gmt 0)

10+ Year Member



I've recently replaced some PHP code that I wrote a long-time ago to force redirects from URLs with no trailing slash to their correct equivalent...

####://mysite.tld/widget

would redirect to...

####://mysite.tld/widget/

The code I placed in .htaccess is as follows:

RewriteCond $1 !^([^/]+/)*[^./]*\.[^/]$
RewriteRule ^(.*[^/])$ ####://mysite.tld/$1/ [R=301,L]

However, my sitemap.xml is no-longer working because it redirects to...

sitemap.xml/

I've already tried adding

RewriteRule ^sitemap.*$ - [PT]

Help!

g1smd

8:48 pm on Jul 21, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Presumably you only want the slash added to extensionless requests, so you could test that the requested URL path does not end in
\.[a-z0-9]$
using an additional RewriteCond.

The .* in your current pattern is inefficient, greedy, promiscuous and ambiguous. You should change it to a more specific pattern.

^(.*[^/])$ simplifies to !/$ but may not be the right pattern for what you want to do.


Have you been having trouble with images on your site? Your rule should have stopped those loading too.

Karma

9:07 pm on Jul 21, 2011 (gmt 0)

10+ Year Member



No, no problem with images.

Could you advise on the actual instructions?

lucy24

11:19 pm on Jul 21, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



RewriteCond $1 !^([^/]+/)*[^./]*\.[^/]$


translates as: the input defined in the Rule (which you don't really need to capture, because htaccess never even looks at the Condition unless the Rule fits) does not consist of:

zero or more occurrences of {one or more non-slashes followed by a slash}, followed optionally by one or more non-slash, non-period characters, followed by a period, followed by a single non-slash.

The entire

^([^/]+/)*[^./]*


part is unnecessary, since it comes down to "there may or may not be some stuff at the beginning of the request". All that matters is the end:

!\.[^/]$


meaning "the request does not end with a literal period followed by exactly one non-slash character". What type of file is this meant to exclude?

Is there anything earlier in your .htaccess that would have picked up the images before they got to this point?

Karma

11:39 pm on Jul 21, 2011 (gmt 0)

10+ Year Member



Thanks for the reply. Yep, I ignore the whole folder...

RewriteRule ^images/.*$ - [PT]

I'm really no expert in the art of .htaccess, think I might just redirect it via PHP :/