In .htaccess, redirect all domains except one

前端 未结 2 1913
温柔的废话
温柔的废话 2020-12-17 15:41

I have multiple domains on my server. I want to redirect all of them to one (example.net).

My .htaccess:

RewriteEngine on 
RewriteRule ^(.*)$ http://         


        
相关标签:
2条回答
  • 2020-12-17 16:19

    You need to add a Rewritecond to prevent it from redirecting when you’re already on the domain that you want. There are loads of examples online if you google it, or see the RewriteCond section of Apache’s mod_rewrite documentation.

    What you’re looking for is something like:

    RewriteEngine on 
    Rewritecond %{HTTP_HOST} !^www\.example\.net
    RewriteRule ^(.*)$ http://www.example.net/$1 [R=301,L]
    
    0 讨论(0)
  • 2020-12-17 16:19

    Just small note: Thanks goes to TRiG, but I had to remove one slash to make it work correctly (coz it added two slashes after domain name). This works for me:

    RewriteEngine on 
    Rewritecond %{HTTP_HOST} !^www\.example\.net
    RewriteRule ^(.*)$ http://www.example.net$1 [R=301,L]
    
    0 讨论(0)
提交回复
热议问题