2015-07-03 45 views
2

我的.htaccess中的代碼將下面的代碼重定向到https,除了一個(/ wc-api/v2),它不能使用SSL。問題.htaccess將所有頁面重定向到HTTPS除了一個

它確實將所有頁面重定向到https,但是如果我轉到/ wc-api/v2,它會將我重定向到/index.php。

# Custom Redirect 
<IfModule mod_rewrite.c> 
RewriteEngine On 
RewriteBase/
RewriteCond %{HTTPS} !^on$ 
RewriteCond %{REQUEST_URI} !^/wc-api/v2 
RewriteRule (.*) https://example.com/$1 [R,L] 
</IfModule> 
# End Custom Redirects 

# BEGIN WordPress 
<IfModule mod_rewrite.c> 
RewriteEngine On 
RewriteBase/
RewriteRule ^index\.php$ - [L] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule . /index.php [L] 
</IfModule> 
# END WordPress 

僅供參考第二塊重定向是Wordpress所必需的。我也無法將它們合併成一個單獨的塊,或者Wordpress會覆蓋我的更改。

回答

4

.htaccess上下文中配置重寫時,在應用上次規則後,整個過程重新開始,使用內部重寫的URL作爲新的「輸入」,直到沒有更多規則匹配。

這意味着,當你請求/wc-api/v2,它沒有改寫爲HTTPS,因爲它沒有你的第一個RewriteCond,因此,在接下來的塊規則將其重寫爲/index.php內部。

現在「下一輪」開始,以/index.php作爲輸入。 RewriteCond %{REQUEST_URI} !^/wc-api/v2現在確實導致「真」,因爲REQUEST_URI不再是/wc-api/v2,現在是/index.php。爲此,該RewriteRule應用 - 你將被重定向到https://example.com/index.php

爲了避免這種情況,你必須添加其他RewriteCond,這將防止應用的REQUEST_URI /index.php以及規則:

RewriteCond %{HTTPS} !^on$ 
RewriteCond %{REQUEST_URI} !^/wc-api/v2 
RewriteCond %{REQUEST_URI} !^/index\.php$ 
RewriteRule (.*) https://example.com/$1 [R,L] 
0

我得到一個錯誤我的建議服務器上的上方,從而修改了它稍微,發現這個工作對我來說:

RewriteEngine On 

RewriteCond %{HTTP:X-Forwarded-Proto} !https 
RewriteCond %{REQUEST_URI} !^/wc-api/v2 
RewriteCond %{REQUEST_URI} !^/index\.php$ 
RewriteRule (.*) https://www.example.com/$1 [R,L] 


# BEGIN WordPress  
<IfModule mod_rewrite.c> 
RewriteEngine On 
RewriteBase/
RewriteRule ^index\.php$ - [L] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule . /index.php [L] 
</IfModule> 

# END WordPress 

希望它爲你工作。

相關問題