htaccess redirect condition doesn't work - .htaccess

I created few redirect conditions but only one among all doesn't work:
Redirect 301 /testy.html?task=quiz&quizId=1 /testy-egzaminacyjne/egzaminy-na-pozwolenie-na-bron.html
It's probably because first (old) URL uses ? = & ... but I can't figure out how it should be changed correctly.

You cannot match query string using Redirect directive. Use mod_rewrite instead.
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^task=quiz&quizId=1$ [NC]
RewriteRule ^testy\.html$ /testy-egzaminacyjne/egzaminy-na-pozwolenie-na-bron.html? [L,R=301]

Related

.htaccess - remove one variable and 301 redirect, but leave others untouched?

I'm trying to remove one entire variable (but only if it's equal to 1). How can I do this? Basically I want to remove the string "&page=1". Here's what I mean:
If page=1:
301 redirect from BEFORE to AFTER
BEFORE: https://www.fubar.com/search.php?cs=tt&st=ss&page=1
AFTER: https://www.fubar.com/search.php?cs=tt&st=ss
If page = 2 or any other number EXCEPT 1 (nothing is changed or forwarded - no 301):
BEFORE: https://www.fubar.com/search.php?cs=tt&st=ss&page=2
AFTER: https://www.fubar.com/search.php?cs=tt&st=ss&page=2
I've tried some suggestions and searches, but nothing seems to be working...
Try with below rule,
RewriteEngine On
RewriteCond %{QUERY_STRING} !page=1$
RewriteRule ^ https://www.fubar.com/search.php?%{QUERY_STRING}&page=1 [R=301,L,END]
RewriteCond %{QUERY_STRING} !page=1$
RewriteRule ^ search.php?%{QUERY_STRING}&page=1 [L]
You can use the following :
RewriteEngine on
# if &page=1
#redirect to remove the &page perm
RewriteCond %{THE_REQUEST} /search\.php\?cs=tt&st=ss&page=1\s [NC]
RewriteRule ^.+$ https://www.fubar.com/search.php?cs=tt&st=ss [NE,L,R]
This will redirect your url from https://www.fubar.com/search.php?cs=tt&st=ss&page=1 to the same url but without the page perameter at the end (ie : https://www.fubar.com/search.php?cs=tt&st=ss ) .
Note that R is a 302 temporary redirect flag . I am using this just to test the rule and avoid browser caches. You can change the R to R=301 to make the redirection permanent .
I found a solution HERE: htaccess 301 redirect rule to remove part of a query string from URLs, but leave the rest)
Here's the code I ended up using:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)&?page=1(.*)$
RewriteRule ^/?search\.php$ /search.php?%1%2 [R=301,NE]

htaccess 301 (permanent) redirection

I need help in 301 permanent URL redirection of dynamic URL's
https://www.xyz.co/certificate.php?certify=iso-haccp
should redirect to
https://www.xyz.co/certificate/iso-haccp-certification
I want to do it using .htaccess file because there are so many url's like this, help me guys?
Try this code :
RewriteEngine On
RewriteCond %{QUERY_STRING} ^certify=(.*)$
RewriteRule ^certificate\.php$ /certificate/%1-certification? [R=301]
A literal redirection would look like this:
RewriteBase /
RewriteCond %{QUERY_STRING} certify=(iso-haccp)
RewriteRule (certificate)\.php /$1/%1-certification? [R=301,L]
We're not using ^ or $ in this example around the query string because it can allow for other variables that could be passed. I'm adding a ? to the end of the redirection string to stop the default behavior of query string append.

htaccess redirect with parameters not working

I'm struggling with an Apache rewriterule. I need to do the following:
Redirect permanently:
http://domain.com/folder/viewer/data/settings.xml?prevent_cache=4760
to
http://domain.com/siteid/includes/themes/siteid/swfs/viewer/data/settings.xml?prevent_cache=4760
I've got the code below, it works without the url parameters but I can't seem to get it to work with parameters. Am i missing something?
RewriteCond %{QUERY_STRING} ^prevent_cache=([0-9]*)$
RewriteRule ^/folder/viewer/data/settings.xml$ http://domain.com/siteid/includes/themes/siteid/swfs/viewer/data/settings.xml [R=301,L]
Cheers
Shaun
The only error I can see, is the leading slash / in the RewriteRule pattern. This should be
RewriteCond %{QUERY_STRING} ^prevent_cache=[0-9]*$
RewriteRule ^folder/viewer/data/settings.xml$ /siteid/includes/themes/siteid/swfs/viewer/data/settings.xml [R,L]
You don't need to append the query string to the substitution URL, because this is done autmoatically.
When everything works as you expect, you can change R to R=301. Never test with 301 enabled, see this answer Tips for debugging .htaccess rewrite rules for details.
I can. Here are the rewrite condition and rule that you're looking for:
# once per htaccess file
RewriteEngine on
RewriteCond %{QUERY_STRING} prevent_cache=([0-9]*)
RewriteRule ^folder/viewer/data/settings.xml http://domain.com/siteid/includes/themes/siteid/swfs/viewer/data/settings.xml?prevent_cache=%1 [R=301,L]
But please considered this answer about the [R=301] flag: https://stackoverflow.com/a/15999177/2007055

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 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