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.
GoTravelAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HackneyCabCommented:
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]
0
GoTravelAuthor Commented:
@HackneyCab: No dice, doesn't work on the non-slash URL's either.  What is going on in "(?:$|/$|/[^/]+$)"?
0
GoTravelAuthor Commented:
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.
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

HackneyCabCommented:
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/.
0
GoTravelAuthor Commented:
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.
0
HackneyCabCommented:
Okay, I had to resort to tampering with my own server's .httaccess file, but this works for me:

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

If you move it to a server-wide httpd.conf file it will need to change, though, as httpd.conf is not directory specific.

This rule allows for www.domain.com/Lodge/ as well, so let me know if you don't permit Lodge to be the first directory in the path.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GoTravelAuthor Commented:
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.
0
HackneyCabCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.