Forum Moderators: phranque

Message Too Old, No Replies

Redirect UPPERCASE url to lowercase via .htaccess

         

pahlawandevisa

6:55 pm on Jan 7, 2012 (gmt 0)

10+ Year Member



Hello,

I'm new commer, and need help to redirect my url to lowercase

example : domain.com/search/1/HELLOWEEN-POWER.html

I want to redirect to domain.com/search/1/helloween-power.html

I had tried this [webmasterworld.com...] but the site not loaded :(

Thanks in advance for any advice.

g1smd

7:23 pm on Jan 7, 2012 (gmt 0)

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



Use a RewriteRule to internally rewrite these requests to a PHP script. The required RegEx pattern is very simple. It matches any request that contains one or more upper characters.

The PHP script then works out what the correct URL will be and sends the correct HTTP HEADER with 301 status code and URL back to the browser.

The internal rewrite needs to be placed before all of your canonicalisation redirects in the .htaccess file to avoid any multiple step redirection chain.

pahlawandevisa

7:59 pm on Jan 7, 2012 (gmt 0)

10+ Year Member



Hello,

thanks for your reply

can you show me an example?

g1smd

9:00 pm on Jan 7, 2012 (gmt 0)

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



This is a question that comes up every few months so there's several previous threads in the Apache forum with the exact code you require.

lucy24

11:29 pm on Jan 7, 2012 (gmt 0)

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



Note that htaccess itself-- that is, mod_rewrite-- can't change casing. It can find upper-case text, but it can't globally change it. (Very few RegEx flavors have this capacity.) So you're doing two things:

First, grab any URL that contains capital letters, and rewrite (not redirect yet) to a php script. Or equivalent language if you don't speak php but know something else that will do the job.

The php script will fix up your URL, and when it's done it will redirect the new improved URL to the place you want.

All this is assuming you've got a whole bunch of URLs that you need to change. If there are only a handful of them, you can do them manually with individual rewrites.

tangor

11:59 pm on Jan 7, 2012 (gmt 0)

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



Welcome to webmasterworld, pahlawandevisa. How many pages do you need to "rewrite"? If not that many, an ordinary 301 will do the job.

pahlawandevisa

3:15 am on Jan 8, 2012 (gmt 0)

10+ Year Member



@tangor,
unlimited url, and some unknown because the site is a search engine

any other solutions?

Thanks

tangor

3:45 am on Jan 8, 2012 (gmt 0)

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



Unlimited? How's that happen? In OP it sounded like you had a URL that is coming with caps from an external query. Return a 404 and move along. Is your site a search engine? (scratching head, or getting confusing signals)

redirectPermanent /index-HUMOR.html http://www.example.com/index-humor.html

lucy24

4:37 am on Jan 8, 2012 (gmt 0)

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



I had tried this [webmasterworld.com] but the site not loaded


Holy ###. He really did it with mod_rewrite alone. I do something vaguely analogous in text files, but honestly, unless your name is jdMorgan I wouldn't advise it. What you're doing in his version is going through the alphabet, changing A to a, B to b and so on, and then looping through the whole thing again, all 26 steps of it, over and over until the whole request rinses clean. So that gives you 26-plus opportunities to go into infinite loops* if you are not very, very careful.**

When you say "the site not loaded" did you mean that the referenced page (jdm's post) didn't load, or that your page didn't load when you tried the code? Did you, in fact, go into an infinite loop?

The original code was posted just about five years ago. Has anyone ever dared use it?


* In related news, I finally worked out why I was still getting the more-than-ten-redirects error even after I'd put in exclusions for the custom 403 document at every stage. It's because some requests meet multiple barriers: if they get past the phony-referer [F], next step is a probably-phony-UA redirect to a different custom page....

** As, for example, if, say, in your hurry to deal with the infinite-redirects problem you don't notice that you have spelled it RewriteCont. Thanks to Murphy's Law of Robotics, several search engines picked that precise moment to come knocking.