Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
478 views
in Technique[技术] by (71.8m points)

.htaccess - htaccess redirect 4 specific pages to https

I have a problem where I need to redirect 4 specific pages on my website to their https secure versions.

I currently have an htaccess file which is redirecting both example.com and www.example.com to https://example.com

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

what I need is something like

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] 

Notice that I have removed the third RewriteCond line from the above code as I don't want every page on my website to be displaying https only the pages I specifically state.

How can I resolve this problem?

PS also, is this line covering both www.example.com and example.com?

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

I'm assuming that

^(.*.)*

has something do with it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
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]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...