We have some crawl errors on our site and I'm trying to write a rewrite rule to fix them. What I want to say is that any url which begins with http://www.mysite.com/co that it should be redirected to a single url.
RewriteEngine On
RewriteBase /
RewriteRule ^co http://www.othersite.com/ [R=301,NC,L]
However, this doesn't work for the following urls
http://www.mysite.com/community/membe%3Cbr%3E%3Chr%3EタグOKです。%3Cbr%3E%3Ctextarea%20name=
http://www.mysite.com/community/membe%3Cbr%3E%3Chr%3E%E3%82%BF%E3%82%B0OK%E3%81%A7%E3%81%99%E3%80%82%3Cbr%3E%3Ctextarea%20name=
Any ideas how I can achieve this?
Thanks
James
Perhaps the easiest way would be whitelisting - redirect anything which contains characters not on your list of allowed characters. In this example, let's say you'd allow only lowercase letters, number 4, and the slash:
RewriteEngine On
RewriteBase /
RewriteRule [^a-z4/] http://example.com/redirect-to-here [R=301,L]
Related
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.
Link example jumppage support:
http://server-address/index.php?param1={value1},{value2},{value3}
-Above is comma separated
Link example our website support:
http://server-address/index.php?param1={value1}¶m2={value2}¶m3={value3}
How can i reformat the initial paramaters are passed according to our standard?
Thanx!
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^param1=(.*?),(.*?),(.*)$
RewriteRule ^index\.php$ index.php?param1=%1¶m2=%2¶m3=%3 [L]
Note this does an internal redirect rather than a 301 or 302. Also not that is the comma separated list contain a variable number of items or there are other parameters, this can also be handled but the rules get a lot more complicated.
I'm trying to transform "domain.com/index.php?site=food&category=beef" into "domain.com/food/beef" but it does not work, no matter what I try. It always leaves the original domain and I get no errors.
I think it's my fault, I tried this for 3 different URLs on 3 different servers (and 3 different projects)... it just seems like I don't get how mod_rewrite really works, though I read every documentation on this topic I found. I even spent days here on SO without finding any solution.
Mod_rewrite is enabled on the server:
RewriteEngine On
RewriteRule ^ http://www.google.com [R,L]
gives me "http://www.google.com/?site=food&category=beef". It looks like mod_rewrite does not recognise the query string... So I tried several solutions with RewriteCond %{QUERY_STRING}... but nothing works :/
Hopefully you guys can help me! I'm going insane on this!
Thanks in advance!
Try:
RewriteEngine on
RewriteRule ^food/beef$ index.php?site=food&category=beef [L]
Or more generally:
RewriteEngine on
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?site=$1&category=$2 [L]
Are you trying to do something like this?
RewriteRule ^([^/]+)/([^/]+)/? /index.php?site=$1&category=$2 [L]
This will make it so when you go to http://domain.com/food/beef the request gets rewritten to "/index.php?site=food&category=beef" internally and index.php is used to serve the original request. The browser's location bar will still say "http://domain.com/food/beef".
If you want the location bar to say http://domain.com/index.php?site=food&category=beef then add an "R," to the "[L]". If this is backwards and you want it so when someone enters http://domain.com/index.php?site=food&category=beef in the location bar, and the request gets rewritten to "/food/beef" internally on the server, then you need to parse out the query string using RewriteCond:
RewriteCond %{QUERY_STRING} ^site=([^&]+)&category=([^&]+)
RewriteRule ^index.php /%1/%2? [L]
The same thing applies with the "R" causing a browser redirect like the first example. If you want the location bar to change to http://domain.com/food/beef then the brackets should look like: [L,R]. Note that you need a ? at the end of the target in the rule, so that query strings don't get thrown in. That is why in your google example, the query string is being appended.
EDIT:
Seeing as you just wanted to change what's in the browser's location bar and not where the content is:
You need to re-rewrite what the 2nd rule above has rewritten BACK to index.php, but without a redirect. In order to keep the 2 rules from looping indefinitely because one rule rewrites to the other rule and vice versa, you need to add a flag somewhere to keep the 2nd rule above from redirecting you over and over again.
So combining the two, you'll have this:
RewriteRule ^([^/]+)/([^/]+)/? /index.php?site=$1&category=$2&redirected [L]
RewriteCond %{QUERY_STRING} !redirected
RewriteCond %{QUERY_STRING} ^site=([^&]+)&category=([^&]+)
RewriteRule ^index.php /%1/%2? [L,R=301]
Note the redirected parameter in the query string. This gets inserted when someone tries to access the clean version of the url, e.g. "/food/beef". internally, it gets rerouted to index.php but since the rule doesn't have a "R", the browser's location bar doesn't change.
The second rule now checks if the request contains the redirected param in the query string. If it doesn't, that means someone entered in their browser's location bar the index.php url, so redirect the browser to the clean version.
i have tried it all!
this:
RewriteCond %{HTTP_HOST} ^www\.(.*)
RewriteRule ^.*$ https://%1/$1 [R=301,L]
works only if i don't put the http at the beggining
how do make that to work:
if there is http redirect to https
if there is www redirect to non-www
and ofcourse both on the same time
http://www.domain.com -> https://domain.com
www.domain.com --> https://domain.com
http://domain.com --> https://domain.com
with every subfolders after and query!
I assume you also want
https://www.domain.com -> https://domain.com
Did you ever get this working? I'm having trouble getting a test https site going to double-check this.
In the meantime, I do see a couple things, so try this instead (this assumes isapi_rewrite v3, which it looks like you're using):
RewriteCond %{HTTP_HOST} ^www\.(.*)
RewriteRule ^(.*)$ https://%1$1 [NC,R=301]
This adds parentheses to the RewriteRule to capture the url for the $1.
Then the slash between the %1$1 isn't needed, since there's one at the start of the $1 capture.
I like to use NC for not case-sensitive, and the R rule is a final rule, so you don't need the L for last.
EDIT
I revisited this answer, to update/clarify a couple of secondary issues in my original answer above. These don't change the main solution above, which was to add the parentheses to do the capture.
Original:
Then the slash between the %1$1 isn't needed, since there's one at the start of the $1 capture.
This actually depends where the rules are, and whether there's a RewriteBase statement. Common shared-host configurations don't have the leading slash here in the rules, so the slash would be needed. If you're not sure, you can try with and without.
Original:
I like to use NC for not case-sensitive, and the R rule is a final rule, so you don't need the L for last.
It turns out it is useful to have L with R for performance, as [NC,R=301,L]. I had actually asked this at Helicon Tech a year before this question, but had forgetten it:
http://www.helicontech.com/forum/14826-Does_Redirect_need_Last.html
From their response:
... the reason for us to use [L] in 301 redirect rules is that redirect occurs not that
immediately. Even though the rule is matched futher rules will be proccessed (run through), unless you have [L]....
I am researching a way to work filtering specific pages by IP and redirect them on a different page.
The code below, did not work properly.
RewriteCond %{REMOTE_ADDR} ^/192.168.10.*
RewriteCond %{REQUEST_URI} ^/support
RewriteRule ^/.* http://www.yahoo.com/gone [R,NE]
Once the link http://example.com/support has been accessed and they're on the 192.168.10.* block, it must go to the yahoo.com example page.
But, like I said. It just did nothing. Any ideas why it did not work correctly?
as yoda says in the comment, don't put a / in front of the ip address. also, the . in the pattern should be \., as this is a perl compatible regular expression. you could also add a [NC], no case (sensitive), to the request uri match. finally, you could merge the second condition with the RewriteRule. all together:
RewriteCond %{REMOTE_ADDR} ^192\.168\.10\..*
RewriteRule ^/support http://www.yahoo.com/gone [R,NE,NC]