Forum Moderators: phranque

Message Too Old, No Replies

Use ModRewrite to replace underscore character

         

techtheatre

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

10+ Year Member



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]

techtheatre

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

10+ Year Member



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?

techtheatre

7:16 am on Dec 6, 2010 (gmt 0)

10+ Year Member



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");
exit();
}

Hope this helps someone

g1smd

1:52 pm on Dec 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



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.