在htaccess RewriteCond中使用多个URL

时间:2015-06-26 23:38:21

标签: apache .htaccess

我在.htaccess文件中使用以下代码,以防止某些网址来自https。如何修改此代码以包含其他网址?例如,我也不希望从https加载about页面。添加类似于职业链接的额外行并不起作用。

<IfModule mod_rewrite.c>
RewriteEngine On
# Go to https if not on careers
RewriteCond %{SERVER_PORT} =80 
RewriteCond %{THE_REQUEST} !/careers/[\s?] [NC,OR]
RewriteCond %{THE_REQUEST} !/capital/[\s?] [NC,OR]
RewriteCond %{THE_REQUEST} !/summer/[\s?] [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

# Go to http if you are on careers
RewriteCond %{SERVER_PORT} !=80 
RewriteCond %{THE_REQUEST} /careers/[\s?] [NC,OR]
RewriteCond %{THE_REQUEST} /capital/[\s?] [NC,OR]
RewriteCond %{THE_REQUEST} /summer/[\s?] [NC] 
RewriteRule ^(.*)$ http://www.example.com/$1 [R,L]
</IfModule>

1 个答案:

答案 0 :(得分:1)

更好地使用正则表达式替换并最小化代码:

<IfModule mod_rewrite.c>
RewriteEngine On
# Go to https if not on careers
RewriteCond %{SERVER_PORT} =80 
RewriteCond %{THE_REQUEST} !/(careers|capital|summer)/[\s?] [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

# Go to http if you are on careers
RewriteCond %{SERVER_PORT} !=80 
RewriteCond %{THE_REQUEST} /(careers|capital|summer)/[\s?] [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R,L]
</IfModule>