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
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]
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.
I am trying to generate an external redirect that involves a few specific tasks.
If specific query string value is found in the URL, redirect.
If redirected, replace one of the query string parameter names but not its value.
If #1 is false, then ignore rewrite and proceed
Example: I have the url http://foobar.com/?a=123&b=456&c=blah
First, if parameter c = blah, redirect to http://barfoo.com/
Second, replace a with x parameter so the final URL is http://barfoo.com/?x=123&b=456&c=blah
Below is my best guess so far after researching on http://mod-rewrite-cheatsheet.com/ and Hidden features of mod_rewrite
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.foobar\.com$ [NC]
RewriteCond %{QUERY_STRING} ^a=(.*)&b=(.*)&c=blah$ [NC]
RewriteRule ^(.*)$ http://barfoo.com/?x=%1&b=%2&c=blah [NC,L,QSA,R=301]
However, the URL is appending the query string, not replacing.
I get redirected to http://barfoo.com/?x=123&b=456&c=blah&a=123&b=456&c=blah
smacks forehead
Removing QSA from the flags solved the issue. QSA means "append the existing query string to the current rewrite rule." It ignores whatever new query string parameters you are adding.
I thought it was needed to have query string parameters exist for the rewrite rule itself.
RewriteRule ^(.*)$ http://barfoo.com/?x=%1&b=%2&c=blah [NC,L,R=301]
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.
So I have some particular variables that mess up the content management system I'm using.
While I debug the code, I'd like to redirect requests with these particular variables back to the index page, but without any query string.
Here's my rule:
RewriteCond %{QUERY_STRING} option\=com\_aicontactsafe\&sTask\=captcha
RewriteRule (.*) "/index.php" [R=301, L]
At this point it is simply writing the file path to index.php after the HTTP_HOST.
This gives a 403, but is not quite what I wanted.
Redirect to /index.php? instead of just /index.php. Normally mod_rewrite doesn't touch the query string, unless you give it a new query string to replace the old one with.