Matching ? character in htaccess - .htaccess

my problem is simply I want to match "?" character but it seems htaccess not matching with ? character. What I want is simply
site.com/search.html?age=1&color=2
to
site.com/index.php?page=search&age=1&color&2
My htaccess
RewriteRule search.html?(.*)$ index.php?page=search&$1 [L]

You cannot match query string in RewriteRule. Besides you don't even need to match ? here.
Replace your rule with:
RewriteRule ^search\.html$ index.php?page=search [L,QSA,NC]
QSA (Query String Append) flag preserves existing query parameters while adding a new one. That's the reason you don't need to capture existing query string here.

Related

Redirect a URL in .htaccess, but keep the query string

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]

Forward QUERY_STRING in .htaccess url rewrite

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

.htaccess need rewrite rule and extra query string

I have the following line in .htaccess:
RewriteRule ^newpdfs/(.*)\.pdf /getnewpdf.php?pdf=$1
getnewpdf.php is a script for adding the name of a user from a session to the filename that gets downloaded. However, there is a scenario where the session hasn't been created and the user's name needs to come from a database. So I need to pass an ID (when needed). I have this:
RewriteRule ^newpdfs/(.*)\.pdf?id=(.*) /getnewpdf.php?pdf=$1&id=$2
However my ID query is getting ignored. How would I go about passing a query string too?
You're missing L (Last) flag and you will need QSA (Query String Append) to append any existing query string. Over your code should be like this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^newpdfs/([^.]+)\.pdf$ /getnewpdf.php?pdf=$1 [L,NC,QSA]
Just add the QSA flag to your existing rule:
RewriteRule ^newpdfs/(.*)\.pdf /getnewpdf.php?pdf=$1 [QSA]
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.
Further reference here: http://httpd.apache.org/docs/2.4/rewrite/flags.html
Add [QSA] to the end of the rule it means to append the query string

mod_rewrite - check for string

I want to check if a URL contains the sting "-EN.htm", if so apply the rewrite.
That should be done with ^-EN.htm as follows, but the rule is not working:
RewriteCond %{REQUEST_URI} ^/(.*?)/([-_0-9a-zA-Z./=]*)^-EN.htm
RewriteRule ^(.*)$ /indexEN.php?folder=%1&follow=%2 [L]
What am I doing wrong?
Thank you for every help,
Scott
Your regular expression doesn't look right. You can also lose the condition and just move the pattern to the rewrite rule instead. Something along the lines of
RewriteRule ^/?(.*?)/([-_0-9a-zA-Z./=]*)^-EN.htm /indexEN.php?folder=$1&follow=$2 [L]
You need to make the leading slash optional (in htaccess this is stripped off) and instead of using % backreferences, use the $ ones.
Now on to your pattern, it's not valid. The ^ matches the beginning of the string (the URI), so if you have two of them and you're not trying to literally match the ^ character (which you'd need to escape), then the expression will never match anything. Without any examples of URLs that you're having to deal with, I assume you probably just want to ditch the second ^:
RewriteRule ^/?(.*?)/([-_0-9a-zA-Z./=]*)-EN.htm /indexEN.php?folder=$1&follow=$2 [L]

mod_rewrite query string

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]

Resources