2017-10-04 107 views
1

我試圖完成兩件事情與htaccess文件:的.htaccess規則 - 重定向過多

  1. 強制使用HTTPS上的所有,但2個目錄,只有從PROD訪問的URL(沒有上開發HTTPS)
  2. 在index.php文件重定向任何不存在的URL

這是我有:

RewriteEngine On 
# Redirect all http to https 
RewriteCond %{HTTPS} !=on 
RewriteCond %{HTTP_HOST} myserver\.com$ [NC] 
RewriteRule ^(api|images)($|/) - [L] 
RewriteRule^https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 
# The following rule tells Apache that if the requested filename 
# exists, simply serve it. 
RewriteCond %{REQUEST_FILENAME} -s [OR] 
RewriteCond %{REQUEST_FILENAME} -l [OR] 
RewriteCond %{REQUEST_FILENAME} -d 
RewriteRule ^.*$ - [NC,L] 
# The following rewrites all other queries to index.php. The 
# condition ensures that if you are using Apache aliases to do 
# mass virtual hosting, the base path will be prepended to 
# allow proper resolution of the index.php file; it will work 
# in non-aliased environments as well, providing a safe, one-size 
# fits all solution. 
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$ 
RewriteRule ^(.*) - [E=BASE:%1] 
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L] 

當我添加第一位(https執行)我得到「太多重定向」錯誤。任何想法爲什麼?

回答

1

一個RewriteCond只適用於未來RewriteRule。您的重定向規則http->https是無條件的,並且會導致重定向循環。

有這樣說:

RewriteEngine On 

# Redirect all http to https 
RewriteCond %{HTTPS} !=on 
RewriteCond %{HTTP_HOST} myserver\.com$ [NC] 
RewriteCond %{THE_REQUEST} !\s/+(?:api|images)[/?\s] [NC] 
RewriteRule^https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE] 

# The following rule tells Apache that if the requested filename 
# exists, simply serve it. 

RewriteCond %{REQUEST_FILENAME} -s [OR] 
RewriteCond %{REQUEST_FILENAME} -l [OR] 
RewriteCond %{REQUEST_FILENAME} -d 
RewriteRule^- [L] 

# The following rewrites all other queries to index.php. The 
# condition ensures that if you are using Apache aliases to do 
# mass virtual hosting, the base path will be prepended to 
# allow proper resolution of the index.php file; it will work 
# in non-aliased environments as well, providing a safe, one-size 
# fits all solution. 
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$ 
RewriteRule ^(.*) - [E=BASE:%1] 

RewriteRule^%{ENV:BASE}index.php [L] 
+1

謝謝你的解釋!那完美的工作。 – LobsterMan

0

我相信這部分應該看起來更像:

RewriteRule ^(api|images)($|/) - [L] 
RewriteCond %{HTTPS} !=on 
RewriteCond %{HTTP_HOST} myserver\.com$ [NC] 
RewriteRule^https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]