Question

i want get full url with parameters to index.php, rewrite with mod_rewrite

.htaccess

RewriteEngine On
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteRule (.*)$ index.php?q=$1 [L,QSA]

index.php is only

print_r($_GET);

when i try

domain.tld/http://asdf/asdf&s=1%3fb=3&c=34

i get

Array
(
    [q] => http://asdf/asdf
    [s] => 1?b=3
    [c] => 34
)

but i need something like this:

Array
(
    [q] => http://asdf/asdf&s=1%3fb=3&c=34 //some complicated url
)

Is there any (simple) way ? I found many solutions for different parameters, not for all-in-one. Sorry for my english :)

Was it helpful?

Solution

You can use this rule in your root .htaccess:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{THE_REQUEST} \s/+(\S+)
RewriteRule ^ /index.php?q=%1 [B,L]

OTHER TIPS

The problem is that, in your example URI, the query string is not well formed, causing Apache to interpret a portion of the string (that part starting with&s=...`) as a query string.

In essence, this means Apache considers this to be the input:

URI => http://asdf/asdf
Query String => s=1%3fb=3&c=34

The redirect rule only operates on the URI portion, and then via QSA flag appends the query string, thus you get something like the following as the rewritten request:

index.php?q=http://asdf/asdf&s=1%3fb=3&c=34

You will need to first URL-encode the query string. For example that might be something similar to the what is produced by the following:

$query_string = urlencode('http://asdf/asdf&s=1?b=3&c=34');

and then to prevent Apache from re-encoding (double-encoding) it, use a NE flag in the redirect rule:

RewriteRule (.*)$ index.php?q=$1 [L,NE,QSA]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top