Welcome to WebmasterWorld Guest from 50.19.53.104

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

lower case and rewrite urls

want to change case in urls

     

tqatsju

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

10+ Year Member



ok i have old urls like this
domain.com/UPPERCASE.html i'd like to rewrite them to
domain2.com/lowercase.html
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

jdMorgan

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

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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:

ETAONIRSHDLFCMUGYPWBVKXJQZ

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

Jim

tqatsju

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

10+ Year Member



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....

tqatsju

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

10+ Year Member



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?

tqatsju

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

10+ Year Member



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]

tqatsju

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

10+ Year Member



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!

jdMorgan

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

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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]

Jim

tqatsju

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

10+ Year Member



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

jdMorgan

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

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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

[httpd.apache.org...]

Jim

tqatsju

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

10+ Year Member



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

tqatsju

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

10+ Year Member



<virtualhost.....
<bunch of host stuff>

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

</VirtualHost>

works like a charm

thanks, JD!

 

Featured Threads

Hot Threads This Week

Hot Threads This Month