Suppose I want to rewrite from
http://example.com/test?data=abcxyz
to
http://example.com/index.php?module=test&data=abcxyz
I tried
RewriteRule ^test?(.*)$ index.php?module=test&$1 [L]
But it doesn't work, the QUERY_STRING becomes module=test&s. (The whole string data=abcxyz become s).
How can I accomplish this task?
Many thanks in advance!
There is a flag to this, the QSA flag :)
RewriteRule ^test?(.*)$ index.php?module=test&$1 [L,QSA]
Explanation from here : Apache doc
When the replacement URI contains a query string, the default behavior
of RewriteRule is to discard the existing query string, and replace it
with the newly generated one. Using the [QSA] flag causes the query
strings to be combin
Related
I want to redirect a URL in .htaccess, but want to keep the (dynamic) parameters from the query string at the end of the URL (e.g. ?id=1660, ?id=1661, etc.)
E.g.
https://mywebsite.example/service/viewinvoice.php?id=1660
I want to redirect it to:
https://mywebsite.example/whmcs-bridge/?ccce=viewinvoice.php?id=1660
So basically: https://mywebsite.example/service/viewinvoice.php?id=... needs to be redirected to https://mywebsite.example/whmcs-bridge/?ccce=viewinvoice.php?id=...
I tried this below, without any success
RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)/service/viewinvoice.php?id= [NC]
RewriteRule ^/?$ /whmcs-bridge/?ccce=viewinvoice.php [L,R=301]
I think this is not the right solution.
Does someone has suggestions?
You need to use the QSA (Query String Append) flag on your rewrite rule. From the documentation:
When the replacement URI contains a query string, the default behavior of RewriteRule is to discard the existing query string, and replace it with the newly generated one. Using the [QSA] flag causes the query strings to be combined.
From your example URLs, you don't need to match the query string in a rewrite condition. You are matching the URL path which is done as the first part of the rewrite rule itself.
Your rule should be:
RewriteEngine On
RewriteRule ^/?service/viewinvoice\.php$ /whmcs-bridge/?ccce=viewinvoice.php [L,R=301,QSA]
Let's say I have phpbb3 forums software and I want to prettify some URLs. I put this in my htaccess:
RewriteRule ^cake viewforum.php?f=5&%{QUERY_STRING} [L]
which works for domain.tld/cake and domain.tld/cake/ but it also catches domain.tld/cake-recipes and domain.tld/cake-recipes/ for instance, and so rewrites them.
How can I write this so that it only matches that exact URL, not URLs that begin with that string?
You need to add $ in order to delimit your rule pattern.
RewriteRule ^cake/?$ viewforum.php?f=5 [L,QSA]
The above rule will now only match domain.tld/cake or domain.tld/cake/.
Also, you can avoid using %{QUERY_STRING} by adding QSA flag (which does the same, but in a more elegant way)
Is possible to rewrite this URL:
http://wwww.mywebsite.com/number_one/
to this URL:
http://www.mywebsite.com/?n=one
in .htaccess?
I'm going to guess that you want to do this using mod_rewrite. In that case, what you need is a RewriteCond to match the query string:
RewriteCond %{QUERY_STRING} ^n=([^&]+)
RewriteRule ^$ number_%1?
(The ? at the end of the RewriteRule tells mod_rewrite to discard the old query string.)
Also note that, as written, this is an internal rewrite. If you want the change to be visible to the user, append the flag [R] (or [R=301] if you want a permanent redirect) to the RewriteRule.
Edit: If you want to go the other way, from site.com/number_one to site.com/?n=one as your question now reads, it's even easier:
RewriteRule ^number_([^/]+) ?n=$1
(As written, this rewrite rule will throw away anything after the first slash following the number, as well as any existing query string parameters. You can keep the original query string by adding the [QSA] flag to the rule if you want.)
I have a query string like
search.php?id=12&keyword=abc&api=gIUTG6898
And I want the URL to be like this:
search/?id=12&keyword=abc&api=gIUTG6898
Now I found a lot of solutions but they are limited to only one variable in the query string. Thanks in advance
It's not that complicated, since you just want to 'transfer' the query string. You can ignore it in the RewriteRule.
RewriteRule ^search/?$ search.php [QSA]
This just rewrites 'search/' to 'search.php'.
Since you are wanting to remove the .php and replace it with a slash, you'd need to reverse Floern's RewriteRule somewhat:
RewriteRule ^/search.php$ /search/ [QSA,L]
I am trying to rewrite this url and it just doesn't seem to be working and I am unclear what is going wrong
I want this URL:
www.mysite.com/dvd-123/Studio/studio_name.php?thumbs=yes&order=price
To actually send to:
www.mysite/cat/list_products.php?studio=123&store=dvd&thumbs=yes&order=price
Here is what I have that is sorta working:
RewriteRule ^dvd-([0-9]+)/Studio/(.*)\.php?(.*)$ cat/list_products.php?studio=$1&store=dvd&$3 [L]
The results I am receiving are the same as if i want to:
www.mysite/cat/list_products.php?studio=123&store=dvd
ignoring the:
&thumbs=yes$order=price
Thank you in advance for your help!
From Apache documentation:
The Pattern will not be matched against the query string... To combine a new query string with an old one, use the [QSA] flag.
I haven't tested this, but hopefully it should point you in the right direction.
RewriteRule ^dvd-([0-9]+)/Studio/(.*)\.php$ cat/user_list_products.php?Manname=$1&store=dvd [L,QSA]
You need to escape the slashes.
RewriteRule ^dvd-([0-9]+)\/Studio\/(.*)\.php?(.*)$ cat/user_list_products.php?Manname=$1&store=dvd&supersale=$3 [L]