문제

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

도움이 되었습니까?

해결책

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_]

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top