2015-04-03 177 views
2

我想重写Apache中的重写规则 - .htaccess。我有一套适用于重定向所有http://到https://的规则。如何使用htaccess正确重定向?

因此,如果用户尝试http://www.example-old.comhttp://example-old.comhttp://www.example-new.comhttp://example-new.com,这一切都将被重定向到https://example-new.com

现在,我想我不希望被重定向到https://example-new.com在服务器上添加一个地址

所以我希望它被排除在下面的代码。如果有人尝试http://newest-example.com,它应该正确地重定向到它。

RewriteCond %{HTTPS} off [OR] 
RewriteCond %{HTTP_HOST} ^www\. [OR,NC] 
RewriteCond %{HTTP_HOST} ^example-old\.com$ [NC] 
RewriteRule^https://example-new.com%{REQUEST_URI} [L,R=301] 

我除了上面的代码试图什么是:

RewriteCond %{HTTP_HOST} ^newest-example\.com$ [NC] 
RewriteRule^http://newest-example.com [R=301,S=1] 

该代码是我所呈现的第一个块的上方。就像你可以看到我试着跳过与S = 1原来重写规则,如果URL是http://newest-example.com但是这给了我这个消息:

Moved Permanently 

The document has moved here. 

Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request. 

不是说这个经历,我不知道我在这里失踪。提前致谢。

回答

2

您可以在第一条规则本身添加排除规则:

RewriteCond %{HTTP_HOST} !^newest-example\.com$ [NC] 
RewriteCond %{HTTPS} off [OR] 
RewriteCond %{HTTP_HOST} ^www\. [OR,NC] 
RewriteCond %{HTTP_HOST} ^example-old\.com$ [NC] 
RewriteRule^https://example-new.com%{REQUEST_URI} [L,R=301] 
+0

只是去尝试和我的服务器引发500错误。甚至我原来的网站都被关闭了。建议?谢谢 – Wolf87 2015-04-03 08:18:58

+0

你可以检查你的Apache error.log并告诉我是什么原因导致500 – anubhava 2015-04-03 09:05:24

+0

这是得到的: 'www.8stepadmission.com [Fri Apr 03 04:21:42 2015] [error] [client 59.154.251.221 ]由于可能的配置错误,请求超过了10个内部重定向的限制。如果需要,使用'LimitInternalRecursion'来增加限制。使用“LogLevel调试”获得回溯.' – Wolf87 2015-04-03 09:57:23