htaccess Elite

Hire htaccesselite professional

.htaccess tutorial


All times are UTC - 5 hours





Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Rewrite urls separated by _ underscores to - dashes
PostPosted: Feb 17th, '07, 21:48 
User avatar

Joined: Sep 24th, '06, 22:48
Posts: 240
Lets say that your site has been using the urls
Code:
http://domain.com/apache_htaccess/apache_htaccess_article_howto.html
http://domain.com/the_best_apache_htaccess_article.html

And you changed the urls to be more SEO friendly by replacing the underscores with dashes.
Code:
http://domain.com/apache-htaccess/apache-htaccess-article-howto.html
http://domain.com/the-best-apache-htaccess-article.html


So how do you automatically 301 redirect requests for the underscore containing urls to the urls with dashes?

Code:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /

RewriteRule !\.(html|php)$ - [S=5]
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4-$5-$6-$7 [E=underscores:Yes]
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4-$5-$6 [E=underscores:Yes]
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4-$5 [E=underscores:Yes]
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4 [E=underscores:Yes]
RewriteRule ^([^_]*)_([^_]*)_(.*)$ $1-$2-$3 [E=underscores:Yes]
RewriteRule ^([^_]*)_(.*)$ $1-$2 [E=underscores:Yes]

RewriteCond %{ENV:underscores} ^Yes$
RewriteRule (.*) http://domain.com/$1 [R=301,L]


Based on article at Replacing a single character with mod_rewrite


Top
 Profile  
 
 Post subject:
PostPosted: Feb 18th, '07, 03:06 
User avatar

Joined: Sep 24th, '06, 22:48
Posts: 240
Alternatively you can try this code

Code:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /

RewriteRule !\.(html|php)$ - [S=5]
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)\.html$ http://domain.com/$1-$2-$3-$4-$5.html [R=301,L]
RewriteRule ^([^_]*)_([^_]*)_([^_]*)_(.*)\.html$ http://domain.com/$1-$2-$3-$4.html [R=301,L]
RewriteRule ^([^_]*)_([^_]*)_(.*)\.html$ http://domain.com/$1-$2-$3.html [R=301,L]
RewriteRule ^([^_]*)_(.*)\.html$ http://domain.com/$1-$2.html [R=301,L]


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB