Ken
Ken

Reputation: 3141

How to properly force HTTPS on specific pages using Apache

I would like to force my signup page to https, and allow all other pages to be browsed using https or http (e.g., http://www.example.com/signup should redirect to https://www.example.com/signup). I was able to force all pages to https, but cannot get only one page to redirect to https. The page just loads normally as http.

Here's the code I've been trying to use in my htaccess file:

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^/signup(/.*)$ https://www.example.com/$1 [R=301,L]

For what it's worth (in case there is a conflict I'm unaware of), I am also using the following code to force all pages to redirect to www and to drop the .php from the file names in the URL:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^([a-z.]+)?example\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .? http://www.%1example.com%{REQUEST_URI} [R=301,L]

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php?/$1

Any thoughts on what I'm doing wrong?

Upvotes: 2

Views: 5014

Answers (1)

anubhava
anubhava

Reputation: 785128

Have your https rule like this:

RewriteCond %{SERVER_PORT} =80
RewriteRule ^(signup/?)$ https://www.example.com/$1 [R=301,L,NC]

Remember that there is no starting slash / in RewriteRule.

Upvotes: 1

Related Questions