Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Hey All, a question about mod-rewrite. When user inputs /somepage?ln=xx my server loads invisibly /xx/somepage which is much a beautiful url.

BUT, those ugly pages still reappear cached and i just wish them to permanently redirect to the nice urls. so they would disappear and only beautifull urls would remain.

/somepage?ln=xx should permanently 301 redirect to /xx/somepage which then would load invisibly the page without showing ugly url. When no language is set, assume english: /somepage should redirect to /en/somepage

In both cases,the file extension should be optional (e.g. should work with and without various extensions) .php .htm .pag, in other words all 3-char-extensions should work too, if those urls have them).

/contact?ln=xx or /contact.zzz?ln=xx
should go both to /xx/contact
language is only 2-chars. What i now have:

# make language as a /lang/page like /xx/contact  WORKS
RewriteRule ^([a-z][a-z])/(.*) /$2?ln=$1 [L]

# when no page, only language set, go to root /nl or /nl/  WORKS
RewriteRule ^([a-z][a-z])/?$ /$1/home [R=301,L]

# permanently redirect ugly urls to nice ones, so the ugly ones dissappear
RewriteRule ???
share|improve this question

1 Answer 1

up vote 0 down vote accepted

So you're redirecting the ugly URL to the nice URL and then rewriting it back again?

I think the trick here is to use RedirectMatch instead of RewriteRule (check you have the module loaded):

RedirectMatch 301 /(.*)?ln=([a-z][a-z]) http://example.com/$2/$1

However, I really don't know how well that will behave when you're essentially creating a loop. It should work, since the Redirect part instructs the browser to request the 'nice' URL, but the Server is then quietly Rewriting it back to the 'ugly' one for processing.

The crux is that if the rewritten URL is then processed again by the redirect, your server may implode into a mini black hole. I'm not sure.

(Edited for clarity.)

share|improve this answer
    
hmmmm interesting. Will try and check back here! Usually, i have found people with humor to be also very intelligent. So my bet is your code will work, unless, ofcourse that implosion into the blackhole could take care of the ugly urls... –  Sam Dec 3 '10 at 18:31
    
nops. nothing changes. Also when changint it to following, nothing happens: RedirectMatch 301 /(.*)?ln=([a-z][a-z]) /$2/$1 somepage?ln=xx just remaind in the browsers url bar and it loads the page correctly without any redirect... indeed, perhaps there is a smarter way than redirecting back and fourth... –  Sam Dec 4 '10 at 0:16
    
That suggests the redirect pattern isn't matching. Are there any other GET vriables following ?ln= part? It's also possible the .* is being too greedy. Try replacing (.*) with ([^?]*). –  SmallClanger Dec 4 '10 at 13:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.