Forum Moderators: phranque

Message Too Old, No Replies

Look for word in URL, then rewrite

         

famouswebteam

3:14 pm on May 17, 2010 (gmt 0)

10+ Year Member



I am trying to rewrite a url if the url contains a certain word.

If the url contains "about.cfm", I want it to rewrite as www.site.com/about

Now it could be just www.site.com/about.cfm, or it could be further in the url like www.site.com/something/else/about.cfm. Either way, I need it to redirect regardless of where the word about.cfm is in the url.

Currently I have this but it doesn't seem to be working well.

RewriteCond %{REQUEST_URI} !/about$
RewriteRule ^about\.cfm$ /about [R=301,L]

Can anyone help? What am I doing wrong?

Thanks!

[edited by: famouswebteam at 3:26 pm (utc) on May 17, 2010]

jdMorgan

3:25 pm on May 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd suggest you look into regular-expressions, specifically "pattern anchoring."

A good concise tutorial is cited by our Apache Forum Charter.

Also, the RewriteCond won't be needed, as the destination URL will not match "about.cfm". Thus there is no danger of a redirection loop.

Specify a full canonical URL as the substitution URL to avoid problems with a non-canonical ServerName and UseCanonicalDomain being set.

RewriteRule ^([^/]+/)*about\.cfm$ http://www.example.com/about [R=301,L]

If "about.cfm" is currently defined as your DirectoryIndex -- e.g. if the purpose of this whole exercise is to "change" about.cfm to an "extensionless" URL, then a different approach is needed, and you should so state.

Jim

famouswebteam

3:26 pm on May 17, 2010 (gmt 0)

10+ Year Member



I may have figured it out:

removing the ^ and $ seems to have made it work.

RewriteRule about\.cfm /about [R=301,L]

Is that correct to do it that way? ie: any danger in doing it like that?

Also, no, the purpose is not just to remove the extension. We have a new site we went live with and I'm working through the 404's, trying to redirect/rewrite them to their appropriate home.

[edited by: famouswebteam at 3:30 pm (utc) on May 17, 2010]

g1smd

3:29 pm on May 17, 2010 (gmt 0)

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



The question contains the word "redirect" and the word "rewrite".

I am not so sure the original poster fully understands the difference between the two - and the differences are crucial to getting this all to work.

For what he wants to do, both of those things are probably required, and in a very specific way.

More comments after the original poster's inevitable further questions. :)

... and no, the above code is not correct. The code posted by jd is what you need.

famouswebteam

3:44 pm on May 17, 2010 (gmt 0)

10+ Year Member



Doing this:

RewriteRule ^([^/]+/)*about\.cfm$ http://www.example.com/about [R=301,L]

Seems to have worked perfectly. Thanks for your help.

g1smd

4:22 pm on May 17, 2010 (gmt 0)

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



It should do! It's the same bit of code posted several hundred times over the last few years.