How to rewrite URLs that cause duplicate content? - .htaccess

I have a lot of links with a double URL structure. Basically, I need a .htaccess rule that redirects all URLs that start /de (for German language) to the same URL with only a /.
example.com/de/Shop/Tradition/Jagd-Forst/
to
example.com/Shop/Tradition/Jagd-Forst/

Your rewrite rule should match starting with /de/ (with an optional strating slash, capture everything afterwards with (.*) and redirect to the capture group ($1) with a 301 permanent redirect:
RewriteEngine on
RewriteRule ^/?de/(.*) /$1 [R=301,L]

Related

.htaccess redirect with questionmark

I want to redirect certain urls matching a pattern to the site root.
I'm doing:
RedirectMatch 301 ^/ABCDE.*$ /
But whenever there is a ? in the url the resulting link is whatever is after the url. How can I redirect also urls including questionmarks?
To strip off existing query string you need to use mod_rewrite rules. Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteRule ^ABCDE.*$ /? [L,R=301,NC]

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

How to remove a ? from a url with htaccess using 301 redirect

I need an htaccess 301 redirect for dynamic urls going from this url:
http://www.example.com/index.php/?content/page1
to
http://www.example.com/content/page1
My current htaccess rule is:
RewriteBase /
RewriteCond %{HTTP_HOST} !=""
RewriteRule ^index.php(/.*)$ http://%{HTTP_HOST}$1 [R=301]
the problem is I get urls like this:
http://www.example.com/?content/page1
How can I remove that question mark (?) from the url. Also this is for about 20 different urls in this pattern. I would like the rule to work for all my urls needing to be 301 in this pattern.
End the target in a ?.
RewriteRule ^index.php(/.*)$ http://%{HTTP_HOST}$1? [R=301]

htaccess: how to redirect every url that starts with http://example.com/shop

Like the title says, how do I redirect every url that starts with http://example.com/shop to a single page?
so I have:
http://example.com/shop/index
http://example.com/shop/detail/52
http://example.com/shop/download
.... etc, and it should be redirected to http://example.com/new_page
You can use mod_alias’ RedirectMatch:
RedirectMatch ^/shop(/|$) /new_page
Or mod_rewrite:
RewriteEngine on
RewriteRule ^shop(/|$) /new_page [R]
Note the different pattern for RewriteRule as mod_rewrite removes the contextual path prefix in per-directory rewrites before testing the rules.

Resources