Redirecting based on query string - .htaccess

Hi all I need to redirect based on query string though htaccess in Apache.
I want that all the queries that contain jjj redirect to my homepage
I used the code below but its not working could you please help me¿
Thanks in advance,
Mauri
RewriteCond %{QUERY_STRING} ^jjj$
RewriteRule (.*) http://www.eventosbarcelona.com [R=301,L]

Your rule redirects example.com/?jjj to example.com/?jjj I guess, You are getting redirect loop error as both urls are same. By default, mod-rewrite appends old query strings to the destination url. You need to use a ? at the end of the Rewrite destination url to discard query string.
Try this :
RewriteEngine on
RewriteCond %{QUERY_STRING} ^jjj$
RewriteRule (.*) http://example.com? [L,R]
This will redirect /?jjj to http://example.com/ .

Related

htaccess dynamically rewrite

example:
https://www.mydomain.de/myproduct/?___store=french&___from_store=german
to
https://www.mydomain.de/myproduct/
I have a store and want to dynamically permanent rewrite storeviews of hundrets of urls to one part of the same url.
And this for many URLs at ones redirecting via 301 to the product part
Can you help me?
You can use this rule to strip off the query string:
RewriteCond %{QUERY_STRING} ___store=
RewriteCond %{QUERY_STRING} ___from_store=
RewriteRule (.*) $1? [R=301,QSD,L]
It checks if the query string containing ___store= and ___from_store=, then redirect to the same URL permanently without the query string.

htaccess redirect url by adding parameters

Need Guidance of 301 redirecting Url through htaccess matching specific format
Existing URL
http://www.example.com/index.php?option=com_toys&limitstart=20
Proposed URL
http://www.example.com/index.php?option=com_toys&view=list&Itemid=2&limitstart=20
Here limitstart may change to 0,20,40,60 to even 1000 and thus should remain same in the new proposed url too
Can anyone advise on to redirect using htaccess of above
You need to match query string using RewriteCond using a regex to capture both parameters:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(option=com_toys)&(limitstart=\d+)$ [NC]
RewriteRule ^/?index\.php$ %{REQUEST_URI}?%1&view=list&Itemid=2&%2 [L,NC,R=301]

301 Redirects problem - urls with ? and =

I'm new with 301 redirects through .htacces.
I can get simple redirects as
redirect 301 /test.html http://www.domain.com/test2.html
to work but I have some urls like this
redirect 301 /test.asp?Group=100 http://www.domain.com/test3.html
and for some reason these don't work.
Thanks.
Here is set of rules for URLs you have provided:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} =group=113 [NC]
RewriteRule ^group\.asp$ http://domain.dk/til-born.htm? [NC,R=301,L]
RewriteCond %{QUERY_STRING} =product=1136 [NC]
RewriteRule ^product\.asp$ http://www.domain.dk/til-born/bukser.html? [NC,R=301,L]
As you can see query string is matched separately to the page name. So .. for each of such redirects you need 2 lines: RewriteCond & RewriteRule.
The rule above will do EXACT match, which means /group.asp?group=113&param=value will not be redirected because query string is group=113&param=value which is more than just group=113.
To have such redirect working (when there are some optional parameters in query string) you have to modify it: RewriteCond %{QUERY_STRING} (^|&)group=113(&|$) [NC] -- this will match group=133 anywhere in query string (group=113 and group=11366 are still different, so no problems here).
This needs to be placed in .htaccess in website root folder. If placed elsewhere some tweaking may be required.
The Redirect directive (as far as I know) matches only on the path, not querystring. Instead, use RewriteRule. The QSA instructs the rewrite engine to append the querystring onto the new redirected URL.
RewriteEngine On
RewriteRule ^test\.asp http://www.domain.com/test3.html [L,R=301,QSA]

.htaccess 301 redirection problem

I am having trouble writing a rule for following problem in .htaccess. Here is what I need.
WHEN URL IS THIS
www.mysite.com/cat/2011/subcat1/subcat2/product.htm?page=2
IT SHOULD 301 REDIRECT TO THIS
www.mysite.com/cat/2011/subcat1/subcat2/product/2.htm
Can you please tell me how to do this?
Thanks
----- EDITED AND TESTED ANSWER ----------
RewriteEngine On
RewriteCond %{QUERY_STRING} page=(\d+)
RewriteRule ^cat/(\d+)/([^/]+)/([^/]+)/product\.htm http://www.mysite.com/cat/$1/$2/$3/product/%1.htm? [R=301,L]
RewriteRule matched only the url part, not the query string. So, RewriteCond is used to match query string.
Please note, matches from the url are used as $N and match from the query string is used as %N
There is a question mark placed in the end of the rewritten url. This prevents the original query string to be added to the new rewritten url.
For any number of subcats:
RewriteEngine On
RewriteCond %{QUERY_STRING} page=(\d+)
RewriteRule ^cat/(\d+)/(.+)/product\.htm http://www.mysite.com/cat/$1/$2/product/%1.htm? [R=301,L]

.htaccess redirect from GET variable to url string

I need to redirect
/search?keywords=somesearchterm
to
/search/somesearchterm
This seems incredibly basic but I've been pounding my head against it for an hour.
Thanks for taking the time to look at this.
You want to implement what is called a "301 Redirect" with mod_rewrite.
RewriteEngine ON
RewriteRule ^/search\?keywords=somesearchterm$ /search/somesearchterm
adding regular expressions:
RewriteEngine ON
RewriteRule ^/search\?keywords=(.+) /search/$1 [R=301,L]
R=301 means provide a 301 Header redirect so the user's URL changes in the browser, and L means don't process any more rewrite rules if this one matches.
If you want to do the reverse -- in other words, if someone goes to mysite.com/search/asearchterm and you want the URL to stay the same, but "behind the scenes" you want it to load a certain server script, do this:
RewriteEngine ON
RewriteRule ^/search/(.+) /search.php\?keywords=$1 [L]
You can not match aginst Query string in RewriteRule directive. Use %{THE_REQUEST} or %{QUERY_STRING} server variables to match the Query string :
The following rule works fine for this redirection
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/search\?kewords=([^&\s]+) [NC]
RewriteRule ^ /search/%1? [NE,NC,R,L]
RewriteRule ^search/([^/]+)/?$ /search?keyword=$1 [QSA,NC,L]

Resources