Ettiene Grobler
Ettiene Grobler

Reputation: 525

htaccess -> need to redirect all www to non-www and one page only to ssl

I've looked "everywhere" but just can't seem to figure this one out.

I need the htaccess to do 2 things for me:

  1. Force all www requests to non-www requests
  2. Also force http (non-ssl) on all pages but one in which case I need ssl

Also, this is a wordpress site using permalinks, so I've got that chunk of htaccess code wordpress also puts in there. But somehow everything works fine, except for when I go to the page that needs to be forced to ssl, then it just redirects to the root of the site.

This is what my code looks like:

RewriteEngine On RewriteBase /

# Redirect from www to non-www
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301]

# Turn SSL on for secure-page
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^secure-page[/]?$
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

# Turn SSL off everything but secure-page
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^secure-page[/]?$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]

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

So results are:

  1. any www and non-www and http urls are redirected to non-www and http -> OK
  2. any www and non-www and https urls (excl. secure-page) are redirected to non-www and http -> OK
  3. secure-page url are redirected to the site root and I can't figure out why -> NOT OK

Can someone please help? Thanks

Upvotes: 2

Views: 1449

Answers (1)

undone
undone

Reputation: 7888

RewriteEngine On
# Redirect from www to non-www
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/secure-page[/]?$
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]


RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/secure-page[/]?$
RewriteRule ^(.*)$  http://example.com/$1 [L,R=301]



# Turn SSL on for secure-page
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/secure-page[/]?$
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

# Turn SSL off everything but secure-page
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/secure-page[/]?$
RewriteRule ^(.*)$  http://%{HTTP_HOST}/$1 [R=301,L]

I've updated my answer!

Upvotes: 2

Related Questions