ISAPI Rewrite Block Requests With Keywords in Query String - isapi-rewrite

How can I block requests with certain keywords in the query string using ISAPI Rewrite.
i.e. lolita, model, girl etc...
Query_String Example:
pid=12263&pid=12263&sourcingName=Qxcfljaw&sourcingEmail=thebest%40hotmail.com&sourcingPhone=76513817313&sourcingQty=87&sourcingComments=What's+the+last+date+I+can+post+this+to++to+arrive+in+time+for+Christmas%3F+http%3A%2F%2Flsmagazinexl.kazeo.com+lolita+model+dads+girl++Wonder+what+was+wrong+with+the+original+sounds+so+they+had+to+replace+them+with+these+obviously+and++ridiculously+fake+ones.%0A+

Was able to accomplish this.
RewriteCond %{QUERY_STRING} .(?:nude|lolita|porn|viagra|cialis|naked|preteen). [NC]
RewriteRule .* - [F]

Related

HTTPS RewriteCond with two / routes

i have n URL like this:
http://name.co/othername/xname
http://name.co/othername/yname
So name.co/othername are equal. Just the 'xname' is changing to a different.
I need a redirect to the HTTPS Side.
I tried this:
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
but the result was not correct. it would be nice if someone could help me
REQUEST_URI will exclude the query string. Also, you need flags, at least [R] and usually [L] as well. You will probably want a 301 redirect in this case, unless you think you are likely to turn this off later. Try replacing the RewriteRule you have with:
RewriteRule (.*) https://%{SERVER_NAME}/$1 [R=301,L]
The R flag means to actually send a redirect to the browser as opposed to just changing how the URL is handled insternally; the L flag means this is the last directive that should be processed; it shortcuts the rest of the processing and avoids any additional changes made by other rules.

ModRewrite - How to configure .htaccess for mod_rewrite for SEO purposes

I'm trying to make some improvements to my e-commerce site to have better Search Engine Optimization. One of the more important things is to setup the product to not use the old:
/detail.php?pid=367
Instead we have setup 8 top level categories with 3 sub categories below each of the 8 top level cats.
So for example, one of those categories would look like thisL
/safety-cabinets-fluid-handling/fluid-drum-carts/
Now what I would like to do is have folders below these categories for each product within. Such as:
/safety-cabinets-fluid-handling/fluid-drum-carts/Lubrigard-Handling-Cart/
My thinking is probably that the best way would be to include the product ID at the end, so it would be this:
/safety-cabinets-fluid-handling/fluid-drum-carts/Lubrigard-Handling-Cart/856
Does this make sense? And what would I put in the .htaccess in this situation?
You can have any URL structure as long as the pattern is found in the ID, not in the URL structure, which is the normal method.
In your question, the pattern could be the fact that the ID is numeric, so it can be captured from any URL that holds it, regardless of it's structure. For example:
http://example.com/cat1/cat2/ID/cat3 or
http://example.com/cat2/ID/cat3/cat1
makes no difference for a rule that captures a numeric string in any position, like this one:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /([\d]+)/?
RewriteRule .* detail.php?pid=%1 [L,QSA]
It will pass silently the ID in the incoming URL to the script, like this:
http://example.com/detail.php?pid=ID
Of course, there can't be any other numeric string before the ID in the URL.
However, if you still want the ID to be fixed in the last position in the URL, just replace the above rule set with this one:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} .*/([0-9]+)/?$
RewriteRule .* detail.php?pid=%1 [L,QSA]

Mod Rewrite + QSA : Same querystring param in rewrite target and in querystring - Force use of target possible?

We have the following rewrite rule:
RewriteRule ^([A-Za-z0-9\_]+)$ index.php?rewrite=$1 [L,QSA]
We were wondering if there was a way to have the ?rewrite=$1 take precedence over one that is passed via the query string in the request uri?
Because as it stands now, due to the QSA flag (which we do need btw) if the following url is hit:
http://www.domain.com/this_rewrite_will_match_the_above_rule?rewrite=some_value
The value of $_GET['rewrite'] in PHP will be some_value, and not this_rewrite_will_match_the_above rule.
Before we go in and start modifying the rewrites and adding a RewriteCond to match the query string, etc etc... We were hoping there was a flag to set so that the target url (index.php?rewrite=$1 in this case) took precedence over the passed query string values.
Hope this makes sense.
Thanks.
Slightly hackish
RewriteRule ^([A-Za-z0-9\_]+)$ index.php?%{QUERY_STRING}&rewrite=$1 [L]
This works because, in php, the second rewrite=... overwrite the first.
I have searched for some mechanics to override single querystring parameters in an Apache rewrite multiple times and quite intensely, but it looks like there is no option to do that, even with the latest Apache version (2.4.3 at the time of this writing).
But there is an alternative that makes use of the fact that the PHP querystring parser only returns the last of multiple idententical querystring parameters.
Example:
http://www.domain.com/index.php?id=123&id=456
This will return the following single (!) entry in $_GET:
array(1) {
["id"]=>
string(3) "456"
}
So you can solve your problem by simply appending any override parameters to the end of your existing querystring (without removing them within the querystring). The last occurrence of a repeated parameter is the one that makes it into the $_GET array.
Unfortunately the QSA switch is not suitable for this technique, as it always appends the original parameters to the end of the new querystring. There is no switch that would preprend the old parameters. So you have to take a little detour with a RewriteCond to catch and prepend the original querystring yourself instead of using QSA:
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^([A-Za-z0-9\_]+)$ index.php?%1&rewrite=$1 [L]
The only function of the RewriteCond is to capture the querystring in %1. The regexp (.*) of the condition is always matched, so the following RewriteRule is always executed.
With this technique your above testcase will rewrite to...
http://www.domain.com/index.php?rewrite=some_value&rewrite=this_rewrite_will_match_the_above_rule
...which will be interpreted by the PHP querystring parser to
$_GET["id"] => "this_rewrite_will_match_the_above_rule"
...which is what you want.
Please be aware that this will work only if you take your querystring values from PHP's $_GET array. It will not necessarily work if you parse the content of $_SERVER["QUERY_STRING"] yourself or if you use any other programming language.
I have opted to create an answer of my own, because its slightly cleaner then the examples provided by Jpsy and Gerben.
Credit where credit is due, their suggestions are what got me here, I only expanded on them:
So, our final solution includes 2 rules.
# check if querystring is not empty (this is the addition vs other answers)
RewriteCond %{QUERY_STRING} !^$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Za-z0-9\_]+)$ index.php?%{QUERY_STRING}&rewrite=$1 [L]
if the above query string fails (mainly, the querystring is empty) this rule will apply.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Za-z0-9\_]+)$ index.php?rewrite=$1 [L]
The reason I opted to go for this dual rule setup is to avoid having the php server variables polluted with "?&rewrite=...." if the query string is empty.
Thanks to jpsy and gerben for the help.
I used the following instructions:
RewriteRule ([^\?]*)\?(.*) $1?$2 [N] (1)
RewriteCond %{QUERY_STRING} (.*?)&?(rewrite=[^&]+)&?(.*)
RewriteRule ^(.*)$ $1?%1&%3 [N] (2)
RewriteRule ^(.*)$ $1?domain=newValue [L,QSA] (3)
The trick is done by the [N] flag in the first two rules, which causes to rewrite engine to re-process the output.
Rule (1) simple rewrites the url as it is. I needed it because I'm using mod_rewrite togheter with mod_proxy_ajp and in the first iteration the query string is not splitted from the url. After the execution of the first line, the url is unchanged but the engine will split the path from the query string.
Rule (2) iterates and removes all occurrences of the parameter "rewrite" from the query string.
Rule (3) sets the new value for the parameter and appends whichever query string survives from the replacement done by rule (2).

RewriteRule domain which contains '?'

I want to have RewriteRule where domain's /page/test or /page/test/ go to www.domain.com. Only for this two example.
I try:
RewriteRule ^/page/test/?$ http://www.domain.com [R=301,L,NC]
This will be good but I have other pages like /page/test/?D_test
and when I put RewriteRule, redirect also /page/test/?D_test. Problem is '?'. How should I write my rule to don't rewrite another page's to www.domain.com ? for example
/page/test/?D_test /page/test/(.*)
I'm not sure I understand your question. You are either asking how can I redirect all the traffic including the query string to the domain and take off the query string. In that case I usually do it this way:
RewriteRule ^/page/test/?$ http://www.domain.com? [R=301,L,NC]
If you want to make it so it doesn't redirect if there is a query string then you have to use a rewrite conditional.
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^/page/test/?$ http://www.domain.com? [R=301,L,NC]

mod_rewrite, change one URL with query to a completely different URL

I am migrating data from one content management system to another. There is no relationship between old URLs and new URLs, although both contain query strings. I am trying to set up a set a rewrites that will redirect broad category lists of data from one to the other.
Here's a sample:
OLD
rss.php?categoryID=53
NEW
index.php?module=news&type=user&func=list&tid=1&filter=blogtopic:eq:19
I tried
RewriteRule ^rss.php\?categoryID=53 index.php?module=news&type=user&func=list&tid=1&filter=blogtopic:eq:19 [L]
but it doesn't match. If I follow that one with
RewriteRule ^rss.php index.php?module=news&type=user&func=list&tid=1 [L]
if DOES match, so I conclude that the question mark in the old URL is causing the problem. I am already escaping the question mark. What do I do?
I will probably end up with about 50 of these in my .htaccess file.
You can't match against the query string (all that stuff after the ?) in a RewriteRule, you need to use a RewriteCond and match against the `%{QUERY_STRING} var:
RewriteCond %{QUERY_STRING} ^categoryID=53$
RewriteRule ^rss\.php$ /index.php?module=news&type=user&func=list&tid=1&filter=blogtopic:eq:19 [L]
Or change the brackets to [R=301,L] if you want to redirect the browser.

Resources