5

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

chocolata
  • 3,258
  • 5
  • 31
  • 60

2 Answers2

6

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_]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • BOOM! You win! This works like a charm. Also, I have replaced `[A-Za-z0-9-_]` with `[\w-]` as per your suggestion. Thanks a lot! Soon I will ask another question about how to use constructions like `/?return=link` (so a secondary querystring) in combination with the rewrite construction displayed in this case. Maybe you could assist there too? :-) – chocolata Dec 03 '13 at 10:20
  • 1
    You're welcome, glad that it worked out. For the new question please leave the link of the question in the comments here and I will sure try my level best. – anubhava Dec 03 '13 at 10:28
4

You just need to add a ? at the end of your target. Can do this with mod_alias as well:

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

however, you'll see a stray ? in the browser's address bar.

If you don't want the stray ?, you'll have to stick with mod_rewrite:

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

(you'll want that before any of the rewrites that you already have.

Jon Lin
  • 142,182
  • 29
  • 220
  • 220