2015-04-03 187 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