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
Related
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>
With apache in the htaccess I would like to replace all periods in the url only when there is an underscore after it.
I want example.com?index.php/blog/low_vs._high to redirect to
example.com?index.php/blog/low_vs_high
Can someone tell me how to do this?
Here is what I have, not sure where to go from here or how to properly debug. Thanks.
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/(.*)\._(.*)$
RewriteRule ^(.*)\.(.*)$ /$1$2 [L,R=301]
You can use this rule:
# execute when there are multiple occurrences of ._
RewriteCond %{REQUEST_URI} ^/(.*)\.(_.*\._.*)$
RewriteRule ^ /%1%2 [L]
# execute when there is only one occurrence of ._
RewriteCond %{REQUEST_URI} ^/(.*)\.(_.*)$
RewriteRule ^ /%1%2 [L,NE,R=301]
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.
I want to change my url from
http://intervideo.2watt.net/component/content/article.html?id=65&Itemid=479
to
http://intervideo.2watt.net/component/content/article.html?id=65&Itemid=332
I have tried the following code but it is not working
RewriteRule ^/?Itemid=479/(.*)$ Itemid=332/$1 [R=301,L]
You cannot capture query string from RewriteRule. Use a RewriteCond instead:
RewriteCond %{QUERY_STRING} ^(id=\d+)&Itemid=479$ [NC]
RewriteRule ^component/content/article\.html$ %{REQUEST_URI}?%1&Itemid=332 [L,NC,R=302]
The current RewriteRule removes any query except query callback for any URL.
# Remove question mark and parameters
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^?#\ ]*)\?[^\ ]*\ HTTP/ [NC]
# Query rewrite exceptions
##RewriteCond %{REQUEST_URI}?%{QUERY_STRING} !^/api.*?callback=.* #does not work
RewriteCond %{QUERY_STRING} !callback=
RewriteRule .*$ %{REQUEST_URI}? [R=301,L]
How to avoid query callback rewrite just from URL ^api\/?([^\/]*)$? Excepted result:
no rewrite for /api?callback=1, /api/user?callback=1, /api/user/2?callback=1
rewrite for /apis?callback=1, /user?callback=1, /api/user?foo=1 etc.
I finally understood your question...
Replace these lines:
##RewriteCond %{REQUEST_URI}?%{QUERY_STRING} !^/api.*?callback=.* #does not work
RewriteCond %{QUERY_STRING} !callback=
with this line:
RewriteCond %{REQUEST_URI}?%{QUERY_STRING} !^/api(/.*)?\?callback=.*
Important notice:
if your script isn't located in the document root, but, i.e., in dir /htest,
and full URL looks like mine: http://localhost/htest/api/?callback=1, then you have to put full path to API in your RewriteCond:
RewriteCond %{REQUEST_URI}?%{QUERY_STRING} !^/htest/api(/.*)?\?callback=.*
You can overcome that by beginning your regex with !/api instead of ^/path/to/api, but /foo/api and /bar/api will be skipped from rewriting too.
Update:
this .htaccess works fine in document root:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^?#\ ]*)\?[^\ ]*\ HTTP/ [NC]
RewriteCond %{REQUEST_URI}?%{QUERY_STRING} !^/api(/.*)?\?callback=.*
RewriteRule .*$ %{REQUEST_URI}? [R=temporary,L]
you may try using it without any other rules to check what is wrong
Update 2
If you have other condition, i.e.,
RewriteRule ^([^.]*)$ index.php?url=$1 [QSA,L]
repeat RewriteCond before it:
RewriteCond %{REQUEST_URI}?%{QUERY_STRING} !^/api(/.*)?\?callback=.*
RewriteRule ^([^.]*)$ index.php?url=$1 [QSA,L]
also to be able to use these rules in /foo subdir, replace ^/api with ^/foo/api
To enable RewriteRule for index.php, need to add in query rewrite exceptions.
This rules works fine and fixes this issue:
# Remove question mark and parameters
RewriteCond %{QUERY_STRING} .+
# Query rewrite exceptions
RewriteCond %{REQUEST_FILENAME} !index.php
RewriteCond %{REQUEST_URI}?%{QUERY_STRING} !/api(/.*)?\?callback=.*
RewriteRule .*$ %{REQUEST_URI}? [R=301,L]