.htaccess one part of URL is user friendly other is query - .htaccess

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.

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]

.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 for htaccess

The URL structure for Joomla 1.5 changed in version 2.5.
Before it was:
http://example.com/index.php?option=com_content&task=view&id=587&Itemid=73
Now it's:
http://example.com/index.php?option=com_content&view=article&id=587&Itemid=114
*Note the id and itemid numbers change based on page and while all pages have an id not all have the Itemid in the url.
While we updated all links in the database lots of people still have the old link structure so we want to edit the htaccess file so if someone enters the old structure it will forward to the right structure.
Simply put any URL entered at example.com with task=view in the url should be replaced with view=article.
Does anyone know a simply way to do this in htaccess, maybe with replace query string method and 301 redirect?
You can match against the query string using mod_rewrite's RewriteCond and the %{QUERY_STRING} variable. Then use the % backreferences in a RewriteRule
Try something like this:
RewriteCond %{QUERY_STRING} ^(.*)&task=view&(.*)$ [NC]
RewriteRule ^(.*)$ /$1?%1&view=article&%2 [R=301,L]

htaccess rewrite urls

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]).

.htaccess: sanitize a query string

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.

Resources