[英]Redirect all urls to https except parked and sub domain with htaccess
[英]Redirect all urls to https with .htaccess, except images (in Symfony2)
我已經嘗試過,但是沒有結果,這是我的.htaccess
。
...
RewriteCond %{HTTP_HOST} ^exampe\.com$ [NC]
RewriteCond %{REQUEST_URI} !(\.png|\.jpg|\.gif|\.jpeg|\.bmp)$
RewriteRule ^(.*)$ https://www.exampe.com/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !(\.png|\.jpg|\.gif|\.jpeg|\.bmp)$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
...
當我嘗試轉到http://example.com/image.jpg
它將在https://example.com
上重定向我
您可以根據THE_REQUEST
制定以下規則:
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !\.(png|jpg|gif|jpeg|bmp)
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} \.(png|jpg|gif|jpeg|bmp)
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
確保在新的瀏覽器中進行測試,以避免舊的瀏覽器緩存。
最好在app/config/security.yml
讓Symfony為您執行此操作
security:
# ...
access_control:
# Force images to use HTTP
- { path: \.(png|jpg|gif|jpeg|bmp)$, roles: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: http }
# Force everything else on HTTPS
- { path: ^/, roles: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https }
您可以在文檔中閱讀有關它的更多信息:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.