Forum Moderators: phranque

Message Too Old, No Replies

Rewrite condition ignoring two prefixes

         

Mike521

10:32 pm on Feb 21, 2012 (gmt 0)

10+ Year Member



This is related to a previous post of mine looking for a way to make sure a trailing slash is appended to urls. I ended up with this code which I found through a thread linked in response to the question:

#add trailing slash if it's omitted
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L,QSA]

Works great but there are some cases where I specifically do not want the trailing slash appended, like if the url is /c/<whatever> or /p/<whatever>

I tried a bunch of things to get it to ignore any url that starts with /c/ or /p/ but all I managed were a few redirect loops and other bad things

Can anyone help me out?

g1smd

10:47 pm on Feb 21, 2012 (gmt 0)

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



Use a preceding
RewriteCond
looking at
REQUEST_URI
and a pattern like
!^[cp]/
or similar.

When using a RewriteRule to invoke a redirect, the rule target should include both the protocol and hostname.

Mike521

2:48 pm on Feb 22, 2012 (gmt 0)

10+ Year Member



thanks g1smd that did the trick, and I added another condition to check the http_host

Just curious, howcome checking the hostname is important? I did some googling and I found something about being careful for other domains that may resolve to the same site.

In our case that will never happen, so is it safe to leave out the host check?

lucy24

9:25 pm on Feb 22, 2012 (gmt 0)

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



Most of the time, regularizing the hostname is for something completely unrelated: avoiding Duplicate Content. You don't want to have

example.com/pagename
www.example.com:80/pagename
www.example.com/pagename

and so on all indexed separately. So each of your Redirects should include the full protocol and hostname, with a catch-all Redirect at the very end to pick up the leftovers.

If you're on shared hosting you may or may not choose to cheat by letting the host do this part for you. There's always a clickbutton somewhere. It depends on whether you can "afford" the extra redirect.

Mike521

3:28 pm on Feb 23, 2012 (gmt 0)

10+ Year Member



I understand, thanks lucy