homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

lower case and rewrite urls
want to change case in urls

 6:31 pm on Jan 11, 2006 (gmt 0)

ok i have old urls like this
domain.com/UPPERCASE.html i'd like to rewrite them to
i wasn't sure the best way to do this, and before i started looking into it further i wanted to see what the best way was

thanks, tom



 7:35 pm on Jan 11, 2006 (gmt 0)

You can do this with mod_rewrite, but it can be a very bad solution, depending on where your code resides.

If you have httpd.conf access, you can use RewriteMap -- see the 'tolower' example in the RewriteMap examples.

If you don't have httpd.conf access and are restricted to .htaccess, then the only solution is the very ugly solution of replacing each character on a case-by-case basis, and restarting mod_rewrite until all characters are replaced. Something like:

# skip next 27 rules if no uppercase in URL
RewriteRule![A-Z] - [S=27]
# else replace uppercase characters
RewriteRule ^([^A]*)A(.*)$ /$1a$2
RewriteRule ^([^B]*)B(.*)$ /$1b$2
... 22 more rules for C through X ...
RewriteRule ^([^Y]*)A(.*)$ /$1y$2
RewriteRule ^([^Z]*)A(.*)$ /$1z$2
# set flag to indicate character replacement, then restart mod_rewrite
# processing to catch multiple uppercase of same characters
RewriteRule .* - [E=changed:yes,N]
# (resume at rule below if all uppercase characters have been replaced
# or if there were no uppercase characters in the original URL)
# if any characters were replaced, do external redirect to update client's URL
RewriteCond %{ENV:changed} ^yes$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

This code should be placed as close as possible to the beginning of your mod_rewrite code, and to the beginning of your .htaccess file. This is because it is necessary to restart mod_rewrite parsing in order to catch multiple occurrances of the same uppercase chararacters.

If these uppercase URLs are standard english words, there may be some benefit to ordering the letter substitutions in most-frequently-used to least-frequently-used order, that is:


This method is horribly inefficient, but will work if you have no other alternatives, such as RewriteMap or a php script.



 8:08 pm on Jan 11, 2006 (gmt 0)

ok so i would just do the redirect normal on the origin server and then on the destination server in the httpd.conf file lower case everything? Never used the httpd.conf rewritemap before sorry....


 8:14 pm on Jan 11, 2006 (gmt 0)

i.e. on the origin server with uppercase urls
RewriteRule ^domain.com/UPPERCASE.html [destinationdomain.com...] [R=301,L]

then on the destination domain convert everything to lower case in the httpd.conf file?


 9:03 pm on Jan 11, 2006 (gmt 0)

hey JD does this look right?

RewriteMap lowercase int:tolower
redirectMatch 301 {lowercase:^(.*)$ http://www.example.com$1

[edited by: jdMorgan at 10:41 pm (utc) on Jan. 11, 2006]
[edit reason] example.com [/edit]


 9:15 pm on Jan 11, 2006 (gmt 0)

sorry for posting so many times and asking without doing it but it's a production server i have to make these changes on and i'm not too sure how i can tinker around with it without messing the server up, since my test server is also using the same config file!


 10:39 pm on Jan 11, 2006 (gmt 0)

Sorry, I can't test this for you...

RewriteMap lowercase int:tolower
RewriteCond $1 [A-Z]
RewriteRule ^/(.*)$ http://www.example.com/${lowercase:$1} [R=301,L]



 11:22 pm on Jan 11, 2006 (gmt 0)

tried that and it didn't break anything when i restarted apache, but it didn't seem to do anything either, is there a special place to put it in the httpd.conf i made sure i put

RewriteEngine on above

but then when i went to urls like

[domain.com...] i got my 404 page not a redirect to [domain.com...] however i have lots of rules in the .htaccess file, does the httpd.conf file execute after the .htaccess or interfere with it in such a way that the statement wouldn't work?

thanks again, tom


 11:48 pm on Jan 11, 2006 (gmt 0)

The code in httpd.conf should run first, as long as it's in the proper place:




 5:21 pm on Jan 12, 2006 (gmt 0)

i read around and since i have a vhosts.conf file, it has to be between the virutalhost brackets i believe for the sites i want it for.....thanks for all the help gonna see if it works


 6:26 pm on Jan 12, 2006 (gmt 0)

<bunch of host stuff>

RewriteEngine on
RewriteMap lowercase int:tolower
RewriteCond $1 [A-Z]
RewriteRule ^/(.*)$ /${lowercase:$1} [R=301,L]


works like a charm

thanks, JD!

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved