How to replace all URLs with "/?h=down" in last address with "#do" and redirect to new address.
example:
http://my.site.com/any/?h=down
redirected to:
http://my.site.com/any#do
You can check for the query string using RewriteCond and then make a redirect using the R flag paired with the NE flag.
NE|noescape
By default, special characters, such as & and ?, for example, will be
converted to their hexcode equivalent. Using the [NE] flag prevents
that from happening.
Doing something like this:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^h=down$
RewriteRule ^(.*)$ $1/#do [R,NE,L]
Since you also like to remove the original query string you could try and add a trailing ? like this:
RewriteRule ^(.*)$ $1/#do? [R,NE,L]
Related
This is so elementary that you have to ask why it is so complex to implement.
Simply need to redirect one url to another on the same domain, the url contains query string aparams though:
I tried:
RewriteRule ^article.php?section=exclusive&id=543 articles/view/4639 [R=301,L]
I get page cannot be found - the redirect is not happening. I have other re-directs in the same htaccess which work. The query string params are causing problems, and escaping them also does not help.
Note the id's of the two urls are not the same and therefore I don't want a clever re-write rule which converts from one url to another with the query strings mapped to new params. I just want a fast and dirty mapping from A to B.
UPDATE
I tried this it redirects but it is adding my old query string to the end of the new url. How can I prevent that?
RewriteCond %{REQUEST_URI} ^/article\.php$
RewriteCond %{QUERY_STRING} ^section=exclusive
RewriteRule ^$ http://www.onlinegooner.com/articles/view/3000 [R=301,L]
Input url is: mydomain.com/article.php?section=exclusive
You may use this rule:
RewriteCond %{QUERY_STRING} ^section=exclusive [NC]
RewriteRule ^article\.php$ /articles/view/3000? [R=301,L,NC]
? in the target will strip off previous query string.
I have old URLs like this:
http://www.foo.org/cgi-bin/search.cgi?Blog=14&tag=my%20long%20tag%20name&limit=100
which I want to redirect to another server's page like:
http://www.bar.org/tag/my-long-tag-name/
On foo.org I have:
RewriteCond %{QUERY_STRING} ^Blog=14&tag=([^&]*)&.*$ [NC]
RewriteRule ^cgi-bin/search.cgi$ http://www.bar.org/tag/%1/? [NC,R=301,L]
This redirects to the correct page on bar.org, where I need to replace the %20s with hyphens. However, it changes every %20 to %2520. I've tried adding the NE flag, but no change. This is the thing I'm stuck with.
Once there, this rule replaces %20s with hyphens:
RewriteRule ^blog/tag/([^\s]*)(?:\s)+(.*)/$ /blog/tag/$1-$2/ [R=301,L]
(Bonus points... some of those original tag values also have %28 and %29 in them, which ultimately I'd like to delete. So a tag of bob%20and%20%28thelma%29 becomes bob-and-thelma.)
You can use these rules in site root .htaccess of www.foo.org:
RewriteEngine On
# temporary rewrite to /tag/tag-name
RewriteCond %{QUERY_STRING} ^Blog=14&tag=([^&]*) [NC]
RewriteRule ^cgi-bin/search\.cgi$ /tag/%1? [L]
# redirect to http://www.bar.org if there is no special char in URI
RewriteRule ^tag/[^\x20\x28\x29]+$ http://www.bar.org/$0 [NC,L,NE,R=301]
# if special char is in the end then simple remove it
RewriteRule ^(tag)/([^\x20\x28\x29]*)[\x20\x28\x29]+$ $1/$2 [NC,N,DPI]
# otherwise replace special char by a hyphen
RewriteRule ^(tag)/([^\x20\x28\x29]*)[\x20\x28\x29]+(.+)$ $1/$2-$3 [NC,N,DPI]
I would like to redirect without looking at the query string, and my redirect result no need append the query string as well, so I add a ? at the end of the RewriteRule.
I tried the following syntax, but the outcome just close to it.
RewriteCond %{QUERY_STRING} .* [NC]
RewriteRule ^exd\.asp$ http://www.example.com/index.php?r=p/consumer? [R=301,L]
and also, i tried to escape the first ?, which I need it, but still the same outcome.
RewriteRule ^exd\.asp$ http://www.example.com/index.php\?r=p/consumer? [R=301,L]
Outcome:
http://www.example.com/index.php?r=p/consumer%3f
I want to get ride of the %3f.
Thanks!
You don't need to append a ? at the end if you already have a query string in your target. Just do this:
RewriteCond %{QUERY_STRING} .* [NC]
RewriteRule ^exd\.asp$ http://www.example.com/index.php?r=p/consumer [R=301,L]
By default, query strings get appended, like this:
RewriteRule ^foo$ /bar [L]
You request /foo?blah and you get /bar?blah
However, if you have a ? in your target, query strings won't get appended unless you have the QSA, so:
RewriteRule ^foo1$ /bar? [L]
RewriteRule ^foo2$ /bar?q=2 [L]
You request /foo1?blah and you get /bar, you request /foo2?blah and you get /bar?q=2. If you include a QSA in the rewrite flags, then &blah gets appended to the end.
first, sorry for my bad English.
I try to rewrite url generated from Form Get and redirect that.
my url is like this:
http://www.mysite.com/properties?action=search&agreement=for-rent&category=my-category&type=&zone=my-zone&city=my-city
and I have this .htaccess configured:
11. RewriteCond %{QUERY_STRING} ^action=(?:[a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)$
12. RewriteRule (.*) %{REQUEST_URI}/%1/%2/%3/%4/%5/? [R=301,L]
So basically all my request are direct to index.php.
21. RewriteCond %{REQUEST_URI} !index\.php|resources|hidden
22. RewriteRule ^(.*)$ index.php/$1 [L]
All works, but the problem is when I have an empty value in query string, the rule add double slash and the above url (for example whit &type=&zone=my-zone... type have empty value) will translate like that:
http://www.mysite.com/for-rent/my-category//my-zone/my-city/
The question is: How can i remove in .htaccess the double slash generated if i have one or more empty value in query string?
Thanks
Easiest is to do another redirect (not real pretty as it requires two 301's).
RewriteCond %{THE_REQUEST} //
RewriteRule .* $0 [R=301,L]
The fun part is that when the url is loaded with a double slash in it, mod_rewrite will automatically remove this. So as you can see above you'll just have to rewrite the url to itself, kind of.
I'm trying to come up with some mod_rewrite to translate http://example.com/?7gudznrxdnu into http://example.com/view.php?id=7gudznrxdnu
But any other page will function properly such as http://example.com/contact and so on.
I think this will work:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^[a-z0-9]+$
RewriteRule ^$ view.php?id=%{QUERY_STRING} [L]
If you want the rewrite to be shown in the browser's address field, you'll have to replace [L] with [L,R=301].
Explanation: The query-string (what's following the question mark) is not part of the URL that RewriteRule sees in its matching-pattern, therefore you can't check for question mark there. In my solution, I run the rule if and only if (RewriteCond) the query string consists solely of a-z and/or 0-9, and my rule only rewrites URLs ending with a slash (except for the query string). I redirect this to view.php?id=, and then append the query string to that.
Edit: Tested on my Apache-server, and I haven't found any bugs (yet).
You should try (in your .htaccess):
RewriteEngine On
RewriteRule ^\?([^/\.]+)?$ view.php?id=$1 [L]