htaccess Elite

.htaccess tutorial


All times are UTC [ DST ]





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

Joined: Sep 25th, '06, 04:48
Posts: 242
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, 09:06 
Offline
User avatar

Joined: Sep 25th, '06, 04:48
Posts: 242
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  
 
 Post subject: Re: Rewrite urls separated by _ underscores to - dashes
PostPosted: Jul 16th, '08, 03:14 
Offline

Joined: Jul 16th, '08, 03:02
Posts: 1
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.


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

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 12 guests


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