Reputation:
I have rewritten a URL with .htaccess so that it looks prettier. But when I add a query string to this URL, the keys and are not visible from PHP.
In my .htaccess file, I tried several things to fix the problem, but nothing worked.
RewriteEngine On
[some code]
RewriteRule ^([a-zA-Z0-9_-]+)/?$ ?site=$1
I have tried this rewriting, to see if it works:
RewriteRule ^search?q=g ?site=search&q=g
But if I print_r($_GET)
in PHP, I will just get Array ( [site] => search )
Try it on http://kwtsports.bplaced.de/search?q=g.
I have a search field that redirects to /search?q=blablaSearchQuery
, but as I said, $_GET['q']
is empty.
This also doesn't work:
RewriteRule ^search?q=([a-zA-Z0-9_-]+)/?$ ?site=search&q=$1
Does anyone know why this happens?
Upvotes: 1
Views: 1020
Reputation: 41838
Does anyone know, why this happens?
Yes. You cannot test the query string in the rewrite rule, so this part of your rule will never work: RewriteRule ^search?q=g
.
If you want to test a query string, you have to test it in the rewrite cond.
For example, for your RewriteRule ^search?q=g?site=search&q=g
example, you would have to do something like this:
RewriteCond %{QUERY_STRING} ^q=([^&]+)
RewriteRule ^search/?$ site=search&q=%1 [L]
In this particular case, since you are not wanting to change the q
value, we don't really need to test it, and you could simply pass it through with the QSA
option after adding site=search
:
RewriteRule ^search/?$ site=search [L,QSA]
Upvotes: 1
Reputation: 6809
Add [QSA]
to the end of your RewriteRule
. This appends the old query string to the result.
See the docs for more information.
Upvotes: 3