So I'm trying to write a rule that will respond with a 404 if certain strings are passed to any of the php scripts. Here's with what I came up with:
RewriteBase /
RewriteCond %{QUERY_STRING} ^.*(string1|string2).*$ [NC]
RewriteRule ^$ [R=404,L]
That rule appears to be matching only www.domain.com/?string1 or www.domain.com/?String2, but not www.domain.com/whatever.php?var=string1 or www.domain.com/directory/script.php?var=string1 or www.domain.com/directory/1/script.php?var=string1 and so on.
Can anyone help and point out what I am doing wrong?
Best,
-Iulian
Your RewriteRule is requiring an empty path. Try it like this:
RewriteBase /
RewriteCond %{QUERY_STRING} ^.*(string1|string2).*$ [NC]
RewriteRule ^.*$ - [NC,L]
As Kevin says, you are requiring an empty URL before the query string, with ^$. You don't need all the .*, you don't have to match the full string. This will work, you don't need the RewriteBase either:
RewriteCond %{QUERY_STRING} (?:string1|string2) [NC]
RewriteRule ^ - [R=404,L]
The ?: just says don't capture this, it's only for grouping. The ^ is a way of matching anything. The - says don't change the URL.
Related
I would like to rewrite everything that does not start with wp-content but ends in .html. My failed attempt:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/wp-content.*
RewriteRule ^(.*)\.html /$1-pages/ [R=301,L]
You don't need RewriteCond here. Just modify next rule to:
RewriteRule ^/?(?!wp-content/)(.*)\.html$ /$1-pages/ [R=301,L]
I used a negative lookahead to negate the results.
I have hundreds of these old links I need to redirect.
Here is one example:
/index.php?option=com_content&view=article&id=433:seventh-character-code-categories-and-icd-10-cm&Itemid=101&showall=1
to
/seventh-character-code-categories-and-icd-10-cm
Essentially I need to remove the /index.php?option=com_content&view=article&id=433: part.
I tried this but I am getting confused with the [0-9] and : parts, so the following does not work:
RewriteRule ^/index.php?option=com_content&view=article&id=[0-9]:(.*)$ /$1 [L,R=301]
Say you want to capture from after : to right before & in the query string you mentioned, then try this expression:
^[^\:]*\:([^\&]*)\&.*$
As #starkeen mentioned in comments, you got to check against the query string. This can be done using RewriteCond %{QUERY_STRING}
So if index.php is in the root folder:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^\/index\.php$
RewriteCond %{QUERY_STRING} ^[^\:]*\:([^\&]*)\&.*$
RewriteRule ^(.*)$ http://example.com/%1 [R=301,L]
Here's another example. This one is for a sub folder:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^\/pages\/index\.php$
RewriteCond %{QUERY_STRING} ^[^\:]*\:([^\&]*)\&.*$
RewriteRule ^(.*)$ /pages/%1? [R=301,L]
Also, notice the ? at the end of the url /pages/%1?, this prevents from re-attaching the query string.
Another thing, captured groups will be set to variables %{number} since set in the RewriteCond.
BTW, depending on your server's configuration, you may need to add the NE flag, like [NE,L,R=301] Plus test whether it is necessary to double escape the literal characters.
what is about direct approach. Skip all till semicolon, mach string till & and replace all with first much
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} [^:]+:([\w-]+[^&]).*
RewriteRule .*$ \/%1? [R=301,L]
</IfModule>
I have the following URL:
http://www.domain.com/index.php?route=product/search&search=product%20keyword%20remove
I would like to remove, "keyword" from this url using htaccess. This is what I have so far but is not working as expected:
RewriteRule ^search=(.*?)keyword(.*?)$ search=$1$2 [L,R=301,NC]
What am I doing wrong here? Also, how should the spaces "%20" be handled? Because at least one of the spaces needs to be removed as well.
There are multiple url that contain "keyword", however the words before and after this keyword might vary, so that's why I tried with (.*)
Thanks
Following rule should work for you:
RewriteEngine On
RewriteCond %{THE_REQUEST} \?(.*search=[^&]*)%20keyword(\S*)\sHTTP [NC]
RewriteRule ^ %{REQUEST_URI}?%1%2 [R=302,NE,L]
to match against a url with query strings, you can also use %{THE_REQUEST} variable.
RewriteCond %{THE_REQUEST} search=(.*?)keyword(.*?) [NC]
RewriteRule ^ /index.php?route=product/search&search=%1%2 [L,R]
%n is part of the regex capture group(s) in the RewriteCond
I've tried the following
RewriteEngine On
RewriteCond %{QUERY_STRING} ^slides=(.*)$
RewriteRule ^/([a-zA-Z\-0-9_]+)/.*$ /$1/ [R=301,L]
for URLS like
http://example.com/whatever-in-here/?slides=asdf to be redirected to http://example.com/whatever-in-here/?
However it isn't working
To strip off query string you can add ? in target:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^slides=(.*)$
RewriteRule ^ %{REQUEST_URI}? [NE,R=301,L]
Also note you don't need to group and capture URI in RewriteRule as you can use %{REQUEST_URI}.
You should use
RewriteRule ^/([a-zA-Z\-0-9_\.\/]+\/\?).*$ /$1/
which will drop everything in the match following the ?. demo
Having this regexp in my .htaccess:
RewriteRule ^thumbnails/([0-9]*)/([0-9]*)/(.*)$ lib/thumb.php?w=$1&h=$2&src=$3 [QSA]
I'm having an issue when passing an url in arguments. The regexp remove all slashes but one. Example:
Enter: domain.com/thumbnails/200/143/http://img.youtube.com/vi/xxxxxxx/0.jpg
Result: domain.com/lib.tuhmb.php?w=200&h=143&src=http:/img.youtube.com/vi/xxxxxxx/0.jpg
Note there is just one slash after http:.
Any ideas?
Thanks!
That is an expected behavior in mod_rewrite since rewrite engine strips multiple / into single / while applying pattern in RewriteRule.
To overcome this behavior use RewriteCond %{REQUEST_URI} to capture your values like this:
RewriteCond %{REQUEST_URI} ^/thumbnails/(\d+)/(\d+)/(.*)$ [NC]
RewriteRule ^ lib/thumb.php?w=%1&h=%2&src=%3 [L,QSA]