.htaccess: sanitize a query string - .htaccess

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.

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]

Redirect subdirectory to query string

I'm not quite sure why this isn't working, but I keep getting either 404s or 500s. I'm trying to redirect subdirectories to the root directory with a query string tacked on. So if I go to example.com/dir, I will be redirected (behind the scenes, obviously) to example.com/?url=dir. I tried using this, but it doesn't seem to work:
RewriteEngine On
RewriteRule [^?](.+) ?page=$1 [L,QSA,NC]
I thought it would get anything after the domain name that doesn't start with a query string (to prevent infinite redirect loops), and send it to the homepage with a query string tacked on. But it isn't working. How can I do this?
RewriteRule [^?](.+) ?page=$1 [L,QSA,NC]
The RewriteRule pattern matches against the URL-path only. This never contains the query string (which is available in a separate server variable: QUERY_STRING).
You should also rewrite to the actual file that is handling the request. If you simply rewrite to ?page=$1 then you are relying on mod_dir to make an additional subrequest for the index document. If the index document is index.php then rewrite directly to that, ie. index.php?page=$1.
The above looks like it will result in a rewrite loop.
Try the following instead:
RewriteCond %{QUERY_STRING} !^page=
RewriteRule (.+) index.php?page=$1 [NS,L,QSA]
The preceding condition checks that the query string does not start with "page=".
The NC is not required here, since you are simply capturing everything, regardless of case.
The NS (nosubrequest) flag prevents the rule being called when mod_dir issues a subrequest for the directory index. In other words, it prevents a request for example.com/ being internally rewritten to index.php?page=index.php.

htaccess query string redirect not working correctly

I have a situation where I want to actually see the url variable even though the rest of my htaccess site uses readable URLS.
The issue is that it is simply showing up as a page not found...
This works...
RewriteRule ^files/(.+)/from_all_files/$ pages/file.php?slug=$1&from=all-files
This does not work
RewriteRule ^files/(.+)?from=all-files$ pages/file.php?slug=$1&from=all-files
Im looking for the second one to work.
You cannot check the query string in a RewriteRule, which can only see the REQUEST_URI. You need to use the following instead:
RewriteCond %{QUERY_STRING} ^from=all-files$ [NC]
RewriteRule ^files/(.+)$ pages/file.php?slug=$1 [QSA,L]
When you request http://example.com/files/some-file?from=all-files, the request will be internally rewritten to pages/file.php?slug=some-file&from=all-files. The Query String Append flag (QSA) will append the current query string to the one you're rewriting to.

Is possible to rewrite /mystring_value/ to ?parameter=value?

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

htaccess: remove selected key=value pair from query string

I've been trying to work my head around this but my .htaccess powers just aren't as good as I thought. I have this sample query string:
http://mycoolsite.com/store.php?a=apples&type=fresh&b=banana
Is it possible to do this using .htaccess:
Check if type=fresh exists. If not then redirect to page index.php
If type=fresh exists, remove it but retain the rest of the query string
You can use mod_rewrite to match against the query string. In your .htaccess:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)type=fresh&?(.*)$
RewriteRule ^(.*)$ /$1?%1%2 [L,R]
This will make it so if someone enters http://mycoolsite.com/store.php?a=apples&type=fresh&b=banana in the browser, their browser will get redirected to http://mycoolsite.com/store.php?a=apples&b=banana . If you want the redirect to happen internally (so the browser's location bar doesn't change), remove the ,R in the brackets at the end of the RewriteRule.

Resources