Link to home
Start Free TrialLog in
Avatar of GoTravel
GoTravel

asked on

Mod-rewrite rule not catching trailing slash in pattern

I have a rewrite rule that is supposed to match:

/Lodge
/Lodge/
/Lodging
/Lodging/

and redirect to:

/Hotels/

The rule is RewriteRule ^(.*)?/Lodg(e|ing)/(.*)?$ $1/Hotels/ [NC,R=301]

It matches without the trailing slash but will not see a url with one.  I have tried the following variations:

RewriteRule ^(.*)?/Lodg(e|ing)/?(.*)?$ $1/Hotels/ [NC,R=301]
RewriteRule ^(.*)?/Lodg(e|ing)/?$ $1/Hotels/ [NC,R=301]

I can't get a PREG tester to tell me what's going on so I'm basically at a loss with this one.
Avatar of HackneyCab
HackneyCab
Flag of United Kingdom of Great Britain and Northern Ireland image

Try this

RewriteRule ^(.*)/Lodge(e|ing)/?$ $1/Hotels/ [NC,R=permanent]

And if you need to allow (but ignore) filenames after the Lodging part, this instead:

RewriteRule ^(.*)/Lodge(e|ing)(?:$|/$|/[^/]+$) $1/Hotels/ [NC,R=permanent]
Avatar of GoTravel
GoTravel

ASKER

@HackneyCab: No dice, doesn't work on the non-slash URL's either.  What is going on in "(?:$|/$|/[^/]+$)"?
Additional, perhaps key note.  If I remove the leading slash so that the rule looks like this:

RewriteRule ^(.*)?Lodg(e|ing)/(.*)?$ $1/Hotels/ [NC,R=301]

Then my trailing slash problem goes away, but I then match any URL with the words 'Lodge' or 'Lodging' at the end.
The (?:$|/$|/[^/]+$) pattern just allows the end, or a slash and the end, or a slash, and then any number of non-slash characters until the end.

Can you list exactly what should be matched. I'm not clear on what is allowed to come before or after the term /Lodge/ or /Lodging/.
www.domain.com/Lodge/ or www.domain.com/Lodging/

OR

www.domain.com/SOMETHING/Lodge/ or www.domain.com/SOMETHING/Lodging/

Anything after should be ignored.

Doesnt "[^/]" mean no slashes?  I thought ^ within a character class was the negation.
ASKER CERTIFIED SOLUTION
Avatar of HackneyCab
HackneyCab
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Quick mod of the rule, possible because this Apache lives on Windows, I removed the leading slash from the redirect: $1Hotels/

With the leading slash it would redirect to include the OS filesystem path, which is an interesting but Bad Thing.  Thank you for the rule and swift production of it.
Glad to be of help.

I'm on Linux, so I'm not sure how Windows handles Apache differently, but it does seem strange that the first slash takes the path to the drive root, rather than the web root. Have you set the DocumentRoot directive so that Apache knows where its web root lies?

Anyway, good luck with your project.