Search code examples
apachehttp-redirectnginxadministrator

Apache redirect for query string


I have a question about Apache redirect. Please see my scenario below.

I have an URL sub.example.com which need to be redirected to www.example.com/index.php?r=something. When I load www.example.com/index.php?r=something in my browser, the css file is getting loaded from www.example.com/css/file.css. So I couldn't write a simple redirect for sub.example.com to www.example.com/index.php?r=something since it will result 404 for css file. I fixed that issues by applying the below redirect rule and it works fine(means css file is also get loaded).

RewriteEngine On
RewriteRule ^(.+/?)?(/|\.htm|\.php|\.html|/[^.]*)$ http://www.example.com/index.php?r=something [P]

My problem is now when I put sub.example.com/?success=1 it should redirect to www.example.com/index.php?r=something&success=1. Does the above rule will satisfy this ? If not can you please help to modify the rule accordingly. Thanks in advance.


Solution

  • After some googling, I found a solution to this. There is a QSA(Query String Append) flag that will force the rewrite engine to append a query string part of the substitution string to the existing string. So I had to add that flag into the existing Rewrite rule as given below.

    RewriteRule ^(.+/?)?(/|\.htm|\.php|\.html|/[^.]*)$ http://www.example.com/index.php?r=something [P,QSA]
    

    And voila, the issue got resolved.

    Thanks,