.htaccess tutorial

htaccess Elite


Rewrite urls separated by _ underscores to - dashes

Ask your mod_rewrite and Redirection questions here, and get answers!

Rewrite urls separated by _ underscores to - dashes

Postby produke » 18 Feb 2007 03:48

Lets say that your site has been using the urls
Code: Select all
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: Select all
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: Select all
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
User avatar
produke
 
Posts: 242
Joined: 25 Sep 2006 04:48

Postby produke » 18 Feb 2007 09:06

Alternatively you can try this code

Code: Select all
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]
User avatar
produke
 
Posts: 242
Joined: 25 Sep 2006 04:48

Re: Rewrite urls separated by _ underscores to - dashes

Postby mdg » 16 Jul 2008 03:14

Hi, I just found this post and it looks like what I need but I can't get it to work.

I am new to this so have patience.

I'm trying this working on my local dev server.

I already have one rewrite rule set up which works.

RewriteEngine On
RewriteBase /acwa/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /acwa/index.php?q=$1 [L,QSA]

However when I add the additional code for the hyphen/underscore rewrite and try the pages I get 404 errors. (It also does strange things to the page layout, but one thing at a time.)

I've tried rearranging things and etc all to no avail.

Any tips as to how to add it so that it works on a local setup?

PS both the existing urls and the new ones have the file extensions removed so I guess I don't need the html extension in the new code anyway.

PPS The site is in a subfolder.

Thanks in advance.
mdg
 
Posts: 1
Joined: 16 Jul 2008 03:02


Return to Redirect or Rewrite Questions