Redirecting SEO rewritten url - .htaccess

I'm using these rewrite rules to go from dynamic urls to static ones.
RewriteEngine On
RewriteRule ^songs/album/([^/]*)\.html$ /index.html?album=$1 [L,QSA]
Old url: http://example.com/index.html?album=rockstar
New url: http://example.com/songs/album/rockstar.html
But if I try to redirect old url to new url, it doesn't work
RedirectMatch 301 ^/index.html?album=(.*)\$ http://example.com/songs/album/$1.html
Any ideas?

The RedirectMatch directive in mod_alias won't match against the query string, only the /index.html part. You'd need to use a RewriteCond ${QUERY_STRING} <regexp> to match against it. But if you redirect like that you're going to cause a loop because the URI is put through the rewrite engine until the URI comes out unchanged:
Person types http://example.com/index.html?album=rockstar into their URL address bar
browser gets redirected to http://example.com/songs/album/rockstar.html
mod rewrite sees /songs/album/rockstar.html and rewrites it to /index.html?album=rockstar
the redirect rule successfully matches against /index.html?album=rockstar
repeat from step 2.
You need to make sure to only redirect if the actual request was for /index.html?album=rockstar, and not when it's been through the rewrite engine:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\?album=(.+)\ HTTP
RewriteRule ^index\.html$ http://example.com/songs/album/%1.html? [R=301,L]
The %{THE_REQUEST} is the actual HTTP request, and not the rewritten URI. The %1 is a backreference to a match in a previous RewriteCond and the ? at the end of the redirect URL tells the RewriteRule not to append the query string to the end.

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 all down.php?name=iptools.online extensions to .html

I would like to automatically redirect down.php?name=NAME on the main page of the site.
For example:
http://example.com/down.php?name=iptools.online
to
http://example.com/iptools.online
I tried with this code:
RewriteRule ^([^/]*)\.html$ /down.php?name=$1 [L]
It works, but when using the old URL, it doesn't redirect.
Try the following before your existing rewrite:
# Redirect "/down.php?name=value" to "/value"
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^name=([^&]+)
RewriteRule ^down\.php$ /%1.html [QSD,R=301,L]
%1 is a backreference to the preceding CondPattern that captures the value of the name URL parameter.
The QSD flag is required to discard the original query string from the request.
The condition that checks against the REDIRECT_STATUS env var is required to prevent a redirect loop.
Test with a 302 (temporary) redirect, before changing to a 301 (permanent) redirect in order to avoid caching issues.
...on the main page of the site.
Note that you must have already updated all your internal links to point to the desired URL. The redirect above is not to redirect internal links, but to redirect search engine crawlers and 3rd party inbound links to the old URLs in order preserve SEO.

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 Redirect not working as expected

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.

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