Rob Fyffe
Rob Fyffe

Reputation: 729

Redirect all Wordpress pages to SSL apart from one

I want to redirect all pages on my Wordpress website to SSL apart from one.

I have tried numerous ways to do this and have succeessfully got the SSL working, but I cannot get the single page exclusion to work.

I am assuming the problem lies with the Wordpress specific rewrites.

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

RewriteCond %{REQUEST_URI} ^parent-page/child-page/
RewriteCond %{HTTPS} on   
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R,L]
</IfModule>
# END WordPress

Any suggestions?

Upvotes: 2

Views: 250

Answers (1)

Jon Lin
Jon Lin

Reputation: 143886

You always need your rules that redirect to be before the rules that internally rewrite. Otherwise, internal stuff (like /index.php) end up getting redirected.

Try:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{REQUEST_URI} !^/parent-page/child-page/
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

RewriteCond %{REQUEST_URI} ^/parent-page/child-page/
RewriteCond %{HTTPS} on   
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R,L]

RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]


</IfModule>
# END WordPress

Note that you also need to add a / leading slash to your %{REQUEST_URI} conditions, those will always start with a /. Additionally, you need to not redirect to https with that URI, so you need to add a negative exclusion condition to the redirect to https.

Upvotes: 4

Related Questions