Nig*_*awk 4 .htaccess mod-rewrite https http
我有以下内容:
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} protected [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !protected [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [L,R=301]
Run Code Online (Sandbox Code Playgroud)
如果目录名为"protected",请确保用户使用https.如果目录是除"protected"之外的任何内容,请确保用户使用http.
这很好用,但如何指定其他目录?
此外,有没有一种方法可以实现这一点,而无需两次指定目录?一次包括它和一次排除它?
谢谢!
UPDATE
虽然我的"受保护"文件夹由于我的规则而被迫使用https,但是对"不受保护"文件夹中的图像,样式表和javascripts的任何引用仍然被重定向到http.这导致"受保护"页面仅部分安全.在重定向代码之前添加以下内容可解决此问题:
RewriteRule \.(css|gif|jpe?g|js|png|swf)$ - [L]
Run Code Online (Sandbox Code Playgroud)
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} protected [NC,OR]
RewriteCond %{REQUEST_URI} protected2 [NC,OR]
RewriteCond %{REQUEST_URI} protected3 [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !protected [NC]
RewriteCond %{REQUEST_URI} !protected2 [NC]
RewriteCond %{REQUEST_URI} !protected3 [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [L,R=301]
Run Code Online (Sandbox Code Playgroud)
你可以OR 用来添加更多选项!
以下是mod_rewrite条件的更多细节: http://httpd.apache.org/docs/current/mod/mod_rewrite.html#RewriteCond