user2715533
user2715533

Reputation: 27

Htaccess Query String Conflicts

I would lile to rewrite urls by passing domain, sub-domain and request uri to a php file by a query string with the following .htaccess

# Apache configuration

    Options -Indexes
    Options -Multiviews
    Options +FollowSymLinks


# Rewrite engine configuration

    RewriteEngine on
    RewriteBase /


# Sub domain redirection (301 redirect)

    RewriteCond %{HTTP_HOST} ^([^\.]+\.[^\.0-9]+)$
    RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]


# Request redirection

    RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.[^\.0-9]+$
    RewriteRule ^(.*)$ index.php?app=%2&sub=%1&req=$1 [QSA,END]

Here are some working examples :

http://dev.testdomain.tld/hello/world

=> /index.php?app=testdomain&sub=dev&req=/hello/world

And

http://www.example.tld/?var=test

=> /index.php?app=example&sub=www&req=/&var=test

But if i have something like this :

http://dev.testdomain.tld/hello/world?app=FAIL&sub=FAIL&req=FAIL

=> /index.php?app=testdomain&sub=dev&req=/hello/world&app=FAIL&sub=FAIL&req=FAIL

The query string will replace domain, sub-domain and request uri variables with FAIL (or everything else from url query string). I could desactivate [QSA] flag, but i would like to keep the original query string, and prevent app, sub, req variables rewriting.

Upvotes: 0

Views: 92

Answers (1)

Stefan Jansen
Stefan Jansen

Reputation: 591

You could remove the app, sub and req parameters from your query string and use the PHP $_SERVER var. This PHP superglobal contains some interesting values to solve your problem:

http://dev.testdomain.tld/hello/world?app=FAIL&sub=FAIL&req=FAIL

var_dump($_SERVER);

Will lead to the following output:

array(...) {
  ["HTTP_HOST"]=>
  string(13) "test.test.tld"
  ["QUERY_STRING"]=>
  string(9) "hahah=w12"
  ["REQUEST_URI"]=>
  string(28) "/~stejanse/tst.php?hahah=w12"
}

You could use these variables to retrieve your information instead of using mod_rewrite.

Upvotes: 1

Related Questions