Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

Use ModRewrite to replace underscore character

6:30 am on Dec 6, 2010 (gmt 0)

Junior Member

10+ Year Member

joined:May 12, 2007
posts: 91
votes: 0

OLD - http://www.example.com/Summer_Camps/Summer_Photos/
NEW - http://www.example.com/Summer-Camps/Summer-Photos/

I have changed all my URLs to use dashes instead of underscores. These URLs are further processed by ModRewrite to actually display page content from a single PHP file. What can I do to replace all underscores in incoming requests with a dash? I have tried both of the following pairs of rules (found elsewhere online) but both return an error 500 on my server (with no additional information about the problem).

#First Version
RewriteRule ^(/.*/[^/]*?)_([^/]*?_[^/]*)$ $1-$2 [N]
RewriteRule ^(/.*/[^/]*?)_([^/_]*)$ $1-$2 [R=301]

#Second Version
RewriteRule ^([^_]*)_([^_]*_.*) $1-$2 [N]
RewriteRule ^([^_]*)_([^_]*)$ /$1-$2 [L,R=301]
6:34 am on Dec 6, 2010 (gmt 0)

Junior Member

10+ Year Member

joined:May 12, 2007
posts: 91
votes: 0

correction...the second version DOES work...but apparently I also made everything lower case (not sure why on that one)...any way to make this force lower case at the same time?
7:16 am on Dec 6, 2010 (gmt 0)

Junior Member

10+ Year Member

joined:May 12, 2007
posts: 91
votes: 0

so...the lower case issue is more complex than i would have imagined. Since i do not have access to the httpd.conf file on my shared hosting server, i could not use the following (does not work in .htaccess):

RewriteEngine On
RewriteMap lc int:tolower
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule (.*) ${lc:$1} [R=301,L]

I ended up adding a line to the template PHP file that i call for every page on my site...

if( $PageUrlName != strtolower($PageUrlName) )
$NewUrl = strtolower($PageUrlName);
$RedirectionUrl = 'http://www.example.com/'.$NewUrl;
header("HTTP/1.1 301 Moved Permanently");
header("Location: $RedirectionUrl");

Hope this helps someone
1:52 pm on Dec 6, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
votes: 0

If there is precisely ONE underscore in the requested URL, the job is simple:

RewriteRule ^([^_]+)_(.*)$ http://www.example.com/$1-$2 [R=301,L]

Check whether the underscore needs to be escaped or not before using this code.

For two underscores, it's:

RewriteRule ^([^_]+)_([^_]+)_(.*)$ http://www.example.com/$1-$2-$3 [R=301,L]

If it's always TWO, you only need one rule.

If it could be one OR two underscores, you need both rules, and processing time increases for every request arriving at the server.

Once it gets to "many" underscores it's time for a completely different approach.

In that case, use .htaccess to rewrite requests with underscores to a special processing script using PHP or whatever your favourite language is.

RewriteRule ^([^_]+)_ /special-script.php [L]

This special script then does all the processing using regular expressions and issues the redirect header.

This special code is only invoked when requests have underscores and therefore does not significantly slow down all of the other page, image, script and stylesheet requests.