htaccess将4个特定页面重定向到https

时间:2012-03-09 12:13:43

标签: .htaccess redirect mod-rewrite url-rewriting

我遇到问题,我需要将网站上的4个特定网页重定向到他们的https安全版本。

我目前有一个htaccess文件,它将example.comwww.example.com重定向到https://example.com

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*\.)*example.com$ [NC]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://example.com/$1 [R] 

我需要的是像

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*\.)*example.com/page1.php$ [NC]
RewriteCond %{HTTP_HOST} ^(.*\.)*example.com/page2.php$ [NC]
RewriteCond %{HTTP_HOST} ^(.*\.)*example.com/page3.php$ [NC]
RewriteCond %{HTTP_HOST} ^(.*\.)*example.com/page4.php$ [NC]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://example.com/page1.php$1 [R] 
RewriteRule ^(.*)$ https://example.com/page2.php$1 [R] 
RewriteRule ^(.*)$ https://example.com/page3.php$1 [R] 
RewriteRule ^(.*)$ https://example.com/page4.php$1 [R] 

请注意,我已从上面的代码中删除了第三条RewriteCond行,因为我不希望我网站上的每个页面都只显示https,而只显示我明确说明的页面。

如何解决此问题?

PS也是,这一行是否涵盖www.example.com和example.com?

RewriteCond %{HTTP_HOST} ^(.*\.)*example.com$ [NC]

我假设

^(.*\.)*

有什么用吗?

2 个答案:

答案 0 :(得分:12)

Options +FollowSymlinks
RewriteEngine On
RewriteBase /

#redirect www.mydomain.com to mydomain.com (or any other subdomain)
RewriteCond %{HTTP_HOST} !^mydomain.com$ [NC]
RewriteRule ^(.*)$ http://mydomain.com/$1 [L,R=301]

#force https for certain pages    
RewriteCond %{HTTPS} !=on
RewriteRule ^(page1\.php|page2\.php|page3\.php|page4\.php)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R]

答案 1 :(得分:4)

我修改并清理了您的代码。您可以在$ DOCUMENT_ROOT:

中的.htaccess文件中使用此代码
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# HTTPS is not already on
RewriteCond %{HTTPS} !=on
# Redirect to HTTPS is URI is page1.php OR page2.php OR page3.php OR page4.php
RewriteRule ^page[1234]\.php$ https://mydomain.com%{REQUEST_URI} [L,R]