301 redirect troubles

I have a site that I just redid and I want to 301 redirect all old URL’s to the new ones.

The old site was: /keyword_keyword_keyword.html the new site is /keyword-keyword-keyword/

So I’m trying to replace underscores with dashes and then replace the .html with a forward slash.

I have this:

DirectoryIndex index.php index.html

# BEGIN WordPress

RewriteEngine On
RewriteBase /
RewriteRule ^index\\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress

# BEGIN 301 Redirects

# REPLACE one underscore at a time until there is one or less left
RewriteRule ^([^_]*)_([^_]*_.*) $1-$2 [N]

# REPLACE the last underscrore and do an external redirect
RewriteRule ^([^_]*)_([^_]*)$ /$1-$2 [L,R=301]

# Replace .html with a /
RedirectMatch 301 (.*)\\.html$ http://http://www.website.com/$1/

# END 301 Redirects

It does not seem to be working as old URL’s redirect to the front page. Any advice is appreciated.

Hi,

You should use this code i think it will help for you

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} \.html$
RewriteRule ^(.*)\.html$ $1 [R=301,L]

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Hmmm, someone merged these two into the same thread. Since they have the same problem, no worries, though!

Gentlemen, your problem is in the ordering of your mod_rewrite statements because the WP code you’re using will direct before converting the _'s to -'s (personally, I’d keep the _'s and convert before taking the code to the query phase but that’s not in your question).

DirectoryIndex index.php index.html

# BEGIN 301 Redirects - [COLOR="#FF0000"]Redirect implies the mod_alias Redirect directive[/COLOR]

[COLOR="#FF0000"]# REPLACE one underscore at a time until there is one or less left
# You're attempting to do two at a time
RewriteRule ^([^_]*)_([^_]*_.*) $1-$2 [N]

# REPLACE the last underscrore and do an external redirect
RewriteRule ^([^_]*)_([^_]*)$ /$1-$2 [L,R=301]

# Replace .html with a /
RedirectMatch 301 (.*)\\.html$ http://http://www.website.com/$1/[/COLOR]

# Rather than repeat code already provided
# in my signature's tutorial article, I'll let you
# go there to fetch the character-by-character
# replacement code from there
# - but it goes BEFORE the WP code!

# Otherwise, if you can guarantee that there will ALWAYS be exactly three _'s
RewriteRule ^([^_]+)_([^_]+)_([^_]+)$ $1-$2-$3 [R=301,L]

# END 301 Redirects

# BEGIN WordPress

RewriteEngine On
[COLOR="#FF0000"]RewriteBase / # where's the Redirect that this undoes? DELETE!
RewriteRule ^index\\.php$ - [L] # already handled by the !-f[/COLOR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress


Despite the errors in your code, I must commend you for removing the WP <IfModule> wrappers which are HIGHLY ABUSIVE of the server!

Regards,

DK

I don’t know anything about this sort of stuff, but one thing I have learned (from you) is to remove the <IfModule> stuff. :stuck_out_tongue:

Thanks for your reply David. :slight_smile:

Hi Ben!

Well, you’ve learned that much - go for the gusto and learn how to use mod_rewrite!

Okay, I’ll leave that alone. At least I know that my rants helped one member!

Did the above post help you with your problem?

[aside]DFW? I’d lived in Grapevine for a while (at Braniff’s zenith many years ago). As the guy in the third seat, I’d hear “and they pay us to do this” and can only wish for another chance at life 'cause “I know what I want to be when I grow up.”[/aside]

Oh, sorry not to have read your “formerly” (if it was there last night) - good to see you again.

Regards,

DK

I am facing the same problem again…any one please help me?

vait,

Put your code BEFORE the WP code. In general, order from most specific rule set to the most generic. Because the WP code will redirect EVERYTHING, it should be last (after you make your _ to - changes).

Regards,

DK