Currently I have the following code in my htaccess:
RewriteEngine On
RewriteRule ^(\w+)\.zip$ generator.php?id=$1 [L]
Current problem: My code drops the currently query (if available), and just pass the id.
I am trying to preserve the url query (it can exists or no) and append the id to the query.
Use the following
RewriteEngine On
RewriteRule ^(\w+)\.zip$ generator.php?id=$1 [L,QSA]
QSA (Query String Append) flag preserves the existing query string from the URL.
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]
For example:
ex.com/read?id=1
should open
ex.com/route.php?action=read&id=1
but url won`t changing.
Try this :
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)\?id=(.+)\sHTTP.*$
RewriteRule ^(.*)$ /route.php?action=$1&id=%2 [QSD,R=301,L,NE]
RewriteCond %{QUERY_STRING} ^action=(.*)&id=(.*)$
RewriteRule ^ /%1?id=%2 [QSD,L,NE]
First I match request that contains query string id=whatever then redirect it with new query string.
Then I match new URI which contains new query string action=whatever&id=whatever and redirect it internally to same original path.
QSD flag is is available in Apache version 2.4.0 and later https://httpd.apache.org/docs/trunk/rewrite/flags.html , I added to discard previous query string and prevent it to be appended.
Clear browser cache then test these rules .
UPDATE:
As per your comment , you want to open /route.php?action=read&id=1 internally while /read?id=1 in browser so , you could do what starkeen answered , but with specific query string and general URI it should look like this :
RewriteEngine on
RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule ^(.*)$ /route.php?action=$1 [QSA,L]
So if query string is existing and strat with id=whatever , /abc?id=123 will get internally from /route.php?action=abc&id=123 and the previous query string will be appending with new one
You can use the following RewriteRule in your /.htaccess :
RewriteEngine on
RewriteRule ^read/?$ /route.php?action=root [QSA,L]
This will rewrite /read?id=1 to /route.php?action=read&id=1 . You do not read to write &id=1 in the Rule's destination as QSA automatically adds it to the Rewrite destination.
I have folder structure like:
- templates
- search.php
- page.php
- index.php
- index.php
It's not full structure, but its enough for this question. Index.php have task to include file based on first query named page. Then, through template class, I get the right template. .htaccess for this part looks like this:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([a-zA-Z0-9\-]+)$ index.php?page=$1
Now comes the part that I can't solve. I have another query, with search string.
index.php?page=search&q=some+query is sample of non-friendly URL. Now, I want to make this URL to look like http://www.domain.com/search?q=some+query.
I tried with RewriteRule ^([a-zA-Z0-9\-]+)?q=([a-zA-Z0-9\+\%]+)$ index.php?page=$1&q=$2 but I get an Undefined index: q in ....
Question is, how can i make first query to act like a static page and second to act like a query string from that page?
You just need a QSA flag in your first rule:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-]+)/?$ index.php?page=$1 [L,QSA]
QSA (Query String Append) flag preserves existing query parameters while adding a new one.
You cannot match query string in RewriteRule directive.
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
I have a link http://mywebsite.com/?view=3457373673863568
everything after the view= will change depending on who gets it.
how can I redirect them to another page on the site i.e. http://mywebsite.com/mypage
something like:
RewriteRule http://mywebsite.com/?view=(?) http://mywebsite.com/mypage/
mod_rewrite parses the URL and the rules only see the path, not the query string. The query string is stored in the variable QUERY_STRING, which needs to be matched separately.
RewriteCond %{QUERY_STRING} ^view=
RewriteRule ^$ /mypage [L]
This matches the empty path (i.e., / on your site) and any query string that starts with view=. The query string will be passed implicitly to the target page.
If you want an external redirect so the user browser shows /mypage, use the [R] flag (change [L] to [L,R]).