301 Redirect not working as expected - .htaccess

I have a strange 301 Redirect problem.
I'm using the following rule
Redirect 301 /catalog/index.php?target=news /news
Oddly, when I visit /catalog/index.php?target=news
I'm redirected to : /catalog/?target=news

The query string isn't part of the URI that the Redirect pattern is matched against. It's removed so you can't attempt to match against it in your statement. You need to use mod_rewrite and a condition that matches against the %{QUERY_STRING} variable:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^target=news$
RewriteRule ^/?catalog/(index\.php)?$ /news? [L,R=301]
Those rules should go in the htaccess file in your document root.

Related

Redirect (301 or 302) for translated page not working

I want to redirect a translated page to another translated page in the htaccess file.
In the original language it works (Redirect 301 /helpie_faq/what-are-my-benefits/ /faq/#hfaq-post-1683), but the redirection of translated pages doesn't work, e.g.
Redirect 301 /helpie_faq/welche-vorteile-erhalte-ich-als-neueinwanderer/?lang=de /faq/?lang=de#hfaq-post-3001
How can I make this work out?
You need to match on the query string, which the redirect directive does not support. Instead you should use RewriteCond on the URI (REQUEST_URI) and on the query string (QUERY_STRING) and a matching RewriteRule.
RewriteEngine On
RewriteCond %{REQUEST_URI} /helpie_faq/welche-vorteile-erhalte-ich-als-neueinwanderer$
RewriteCond %{QUERY_STRING} ^lang=de$
RewriteRule ^.*$ /faq/?lang=de#hfaq-post-3001 [L,R=301]
For more examples look here Apache Redirect 301 fails when using GET parameters, such as ?blah= or here Redirecting URLs (with specific GET parameters)

redirect 301 with query string doesn't seem to work

I need to redirect some URL with query string to a different URL
like
redirect 301 /web/works/?id=1 https://www.example.com/it/my_work_new_page/
.htaccess seem to ignore this
but if i add
redirect 301 /it/fake/ http://www.test.it
it works
I already have a file with about 100 of this redirect
You cannot match query string using Redirect directive. Use mod_rewrite with a RewriteCond directive like this:
RewriteEngine On
RewriteCond %{THE_REQUEST} /web/works/\?id=1\s [NC]
RewriteRule ^ https://www.example.com/it/my_work_new_page/? [L,#=301]
? in the target will strip previously existing query string.
References:
Apache mod_rewrite Introduction
Apache mod_rewrite Technical Details

htaccess redirect adds query and original URL to redirected page

I've tried using each of the following
Redirect 301 /example http://website.com/new-page
RedirectMatch 301 /example(.*) /new-page/$1
RewriteRule ^example/(.*)$ http://website.com/new-page$1 [R=301,L]
In each case the final URL resolves to "website.com//new-page//?q=/example/"
Note that it is adding /?q=/example/ to the final URL.
That might be due to other rules in your .htaccess. Use this rule to capture original string from THE_REQUEST variable. And add ? in the end to strip off query string.
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+example(\S*)\s [NC[
RewriteRule ^ http://website.com/new-page/%1? [R=301,L,NE]

301 redirects: match querystring, don't append it to new redirect

I've set up a number of 301 redirects in an .htaccess file, but I'm having problems with a query string that makes the redirect not match.
Example:
Redirect 301 /about/history/?lang=fr http://www.newdomain.com/fr/history
Redirect 301 /about/history/ http://www.newdomain.com/nl/history
So olddomain.com/about/history/?lang=fr now matches the second rule and redirects to http://www.newdomain.com/nl/history?lang=fr.
I want it to take the ?lang=fr literally and not append the querystring to the new redirect.
How do I do that?
Redirect takes an URL-path, which doesn't include the query string. So, the first Redirect never matches.
To achieve what you want, you can try some sort of content negotiation or use mod_rewrite
RewriteEngine on
RewriteCond %{QUERY_STRING} lang=fr
RewriteRule /about/history/ http://www.newdomain.com/fr/history? [R,L]
RewriteRule /about/history/ http://www.newdomain.com/nl/history [R,L]
When everything works as you expect, you can change R to 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]

Resources