Forum Moderators: phranque

Message Too Old, No Replies

Redirect specific URLs with catch all

Help with .htaccess redirects

         

bucho_ky

12:01 pm on Feb 12, 2009 (gmt 0)

10+ Year Member



Hello guys, new here.

I've picked through many .htaccess posts here and hacked around most of the night with no luck. I'm new to .htaccess rules.

I am migrating a live website from .html to Wordpress and want to send all requests from www.domain.com to www.domain.com/travel

I have put specific rules in .htaccess for each URL (about 40 total) that look like this:

RewriteRule ^site-map\.html$ /travel/site-map/ [R]
RewriteRule ^contact\.html$ /travel/contact/ [R]

and etc....

They work great, but after the 40 specific rules I need a simple catch all that will send anything else www.domain.com that I don't know about to www.domain.com/travel.

I've tried several different options and my catchall works great but ends up trashing the rules for the specific URLs I made before.

Any help is appreciated - cheers!

jdMorgan

3:00 pm on Feb 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You did not use the [L] flag on any of your rules, apparently...

Did you read the Apache mod_rewrite documentation? It's a good idea, unless you like "trashing your site"... Using mod_rewrite without reading the instructions can be quite deadly to your server function, search results rankings, traffic, revenue, and business. See the resources cited in our Apache Forum Charter.

There is no need to use 40 rules if all you want to do is redirect all .html pages to a subdirectory and remove the trailing ".html". This problem could probably be handled by a single rule, possibly adding RewriteConds later if needed to create exceptions for special cases:


RewriteRule ^([^.]+)\.html$ http://www.example.com/travel/$1/ [R=301,L]

Jim

bucho_ky

8:15 pm on Feb 12, 2009 (gmt 0)

10+ Year Member



Thanks Jim,

RewriteRule ^([^.]+)\.html$ http://www.example.com/travel/$1/ [R=301,L]

worked just fine after I removed the $1 at the end - perfect!

I agree, and wish I didn't have to use the individual rules, unfortunately during this migration many URLs are being changed for SEO so unlike my 2 examples they don't always match up to their .html equivalents.

I will keep an eye on the server and Apache. Thankfully, .htaccess should not require any more tinkering beyond this.

Thanks for your help!
- Greg

g1smd

8:37 pm on Feb 12, 2009 (gmt 0)

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



Make sure you have [L] on each of your other RewriteRules, and be aware that [R] generates a 302 redirect. You'll likely need to change the code to make each one a 301 redirect.