0

I did my best to search in stack overflow questions and answers and even when I tried to apply a solution, it is not working.

I need to redirect all adresses, that begin with

http://www.example.com/index.php?lang=XX

to

http://www.example.com/XX

This is what I try:

RewriteCond %{HTTP_HOST} ^www.example\.com [NC]
RewriteRule ^(index\.php\?lang\=)$ http://www.example.com/$1 [L,R=302]

The location /index.php?lang=en is not redirecting /en (anywhere). It is at the same URL.

5
  • By not working what do you mean? The user still ends up at example.com/index.php?lang=en - is this what you mean? Commented Sep 22, 2016 at 19:18
  • I have to be completely confused. I need to end up at example.com/en Commented Sep 22, 2016 at 19:20
  • Yes, but where is the user currently ending up? At example.com/index.php?lang=en (so in other words, no redirect) ? Commented Sep 22, 2016 at 19:21
  • Yes exactly, it is not redirected. Sorry I was not clear. Commented Sep 22, 2016 at 19:23
  • 2
    To rewrite a request based on the contents of the query string, have a look at the Apache httpd wiki article on this topic: wiki.apache.org/httpd/RewriteQueryString In short, RewriteRule has access to the requested URI, not including the QUERY_STRING. For that, you need RewriteCond. Commented Sep 22, 2016 at 19:25

1 Answer 1

1

You can't match a query_string in a rewriterule. You need a specific RewriteCond for it. It should be more or less like this:

RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteCond %{QUERY_STRING} ^lang=(.*)
RewriteRule ^ http://www.example.com/%1 [R=302,L,QSD]
Sign up to request clarification or add additional context in comments.

5 Comments

Correction: It IS working now, after you've updated the answer. Thank you. I'll check it precisely and come back.
The ^ indicates the start of the string, nothing else is needed since you are not capturing anything and yes I added a QSD (query string discard) to avoid a rewrite loop. Thanks to @Rich Bowen for reminding it to me.
Thank you for perfect answer: ezra-s and @RichBowen for useful contribution as well.
@ezra-s you actually don't need to use QSD, You can simply use the ? to discard query string. http://www.example.com/%1?
@PanamaJack Didn't know that, thanks for sharing it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.