Forum Moderators: phranque

Message Too Old, No Replies

Page Renaming and 301

Trying to automate the 301 code

         

Landyman

6:21 pm on Mar 16, 2004 (gmt 0)

10+ Year Member



Hi, this topic has probably been posted before, but scouring through the forums, I couldn't find exactly what I want to do.
What I have is a bunch of pages that are in this format:
Second_First.html
and I want to name them:
First-Second.html

Since these pages have rankings, I need to do a 301 redirect to keep them that way. Is there a good automated way to do this redirection? Or am I going to have to do "RedirectPermanent /Second_First.html [domainname.com...] for all of the pages I have. Any help is greatly appreciated. Thank You!

Birdman

8:16 pm on Mar 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can do this with mod_alias or mod_rewrite. These examples should redirect(301) all files in the root folder that have the underscore present.

Be sure to have your backup .htaccess handy as I've been known to give an incorrect example ;)

mod_alias
RedirectMatch 301 ^(.*)_(.*)\.html$ ht*p://www.yoursite.com/$2-$1.html

mod_rewrite
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)_(.*)\.html$ /$2-$1.html [R=301,L]

Landyman

8:36 pm on Mar 16, 2004 (gmt 0)

10+ Year Member



Ok. Thanks! Does this also work for directories?
Like if I have /Directory/Second_First.html and want to make it /Directory/First-Second.html.. can I do this?

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)/(.*)_(.*)\.html$ $1/$3-$2.html [R=301,L]

Thanks for the help so far!

Landyman

9:13 pm on Mar 16, 2004 (gmt 0)

10+ Year Member



Thanks!
I went with the mod_alias that you gave me.
The syntax I used is:
RedirectMatch 301 ^/(.*)/(.*)_(.*)\.html$ [domainname.com...]
And this kept the directories and everything too.
Works like a charm.
Thanks for your help!

Birdman

9:15 pm on Mar 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, that should work just fine.

Birdman

9:24 pm on Mar 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ah, see I told you I give incorrect examples ;) I forgot that mod_alias starts with the slash at the beginning of the filepath, whereas mod_rewrite does not.

But, I think you are going to need two lines because your example should only redirect URLs in the second level directories. You will still need one for the files in the root directory.

RedirectMatch 301 ^/(.*)_(.*)\.html$ h*tp://www.domainname.com/$1/$3-$2.html
RedirectMatch 301 ^/(.*)/(.*)_(.*)\.html$ h*tp://www.domainname.com/$1/$3-$2.html

Landyman

12:35 am on Mar 17, 2004 (gmt 0)

10+ Year Member



Yep. Got it. Thanks for all your help!