Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.htaccess 301 redirect all pages within directory to other domain without querystring

This question probably has been answered, but I cannot seem to find a fitting solution.

I would like to 301 redirect all pages like the ones below

http://www.domain1.com/nl/dolor/sith
http://www.domain1.com/nl/loremipsum
http://www.domain1.com/nl

To a new domain, and at the same time drop the querystring, like so:

http://www.domain2.nl

All other pages, such as http://www.domain1.com/be/loremipsum should still work. Only the ones with suffix nl should redirect.

Please note that these are not real directories: in my .htaccess file I've got the following statements to rewrite my query string:

# Personal Rewrites
RewriteRule ^([A-Za-z0-9-_]+)/?$                                                                                            index.php?lid=$1                                        [L] 
RewriteRule ^([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/?$                                                                           index.php?lid=$1&pl1=$2                                 [L] 
RewriteRule ^([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/?$                                                          index.php?lid=$1&pl1=$2&pl2=$3                          [L] 
RewriteRule ^([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/?$                                         index.php?lid=$1&pl1=$2&pl2=$3&pl3=$4                   [L] 
RewriteRule ^([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/?$                        index.php?lid=$1&pl1=$2&pl2=$3&pl3=$4&pl4=$5            [L] 
RewriteRule ^([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/([A-Za-z0-9-_]+)/?$       index.php?lid=$1&pl1=$2&pl2=$3&pl3=$4&pl4=$5&pl5=$6     [L] 

I've tried the traditional rewrite, but this also sends the querystring:

Redirect 301 /nl http://www.domain2.nl

Other techniques do not seem to work. And I'm not good at regexes...

Could someone give or link to a fitting solution? Thanks in advance

like image 714
chocolata Avatar asked Nov 21 '13 18:11

chocolata


1 Answers

Add this rule as the first rule in your DOCUMENT_ROOT/.htaccess:

RewriteRule ^nl(/.*|)$ http://www.domain2.nl/? [L,R=301,NC]

Another tip about regex:

You should change your regex to use: [\w-] instead of [A-Za-z0-9-_] since:

  1. Hyphen should be first or last characcter in character class to avoid escaping
  2. \w is equivalent of [a-zA-Z0-9_]
like image 192
anubhava Avatar answered Sep 27 '22 19:09

anubhava