2

For some reason my redirects are not working. I just want non-www to go to www and non-https to go to https. Here's what I've got. It's from this post.

RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule (.*) https://www.example.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteCond %{HTTP:X-Forwarded-Proto} !=https
RewriteRule (.*) https://www.example.com/$1 [R=301,L]

EDIT: Here is my whole file.

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On

RewriteBase /
RewriteCond %{HTTPS} Off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTP_HOST} blog.example.com
RewriteRule ^(.*)$ https://www.example.com/blog/$1 [R=permanent,L]

RewriteCond %{HTTP_HOST} ^example.com/blog/$ [NC]
RewriteRule (.*) https://www.exmaple.com/blog/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^www.example.com/blog/$ [NC]
RewriteCond %{HTTP:X-Forwarded-Proto} !=https
RewriteRule (.*) https://www.example.com/blog/$1 [R=301,L]

RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
frameworkgeek
  • 203
  • 3
  • 13

1 Answers1

3

I just want non-www to go to www and non-https to go to https.

What you had initially was close, but it depends on how the SSL cert is managed. If the SSL is managed by a front-end proxy (eg. Cloudflare flexible-SSL/FREE option) then it maybe OK. However, if the SSL cert is installed directly on your server then probably not.

Try something like:

# Non-www to www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) https://www.%{HTTP_HOST}/$1 [R,L]

# Non-SSL to SSL
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R,L]

This assumes you only have example.com and www.example.com, and no other subdomains. However, in your edited code you make reference to a blog subdomain - this will be OK, provided you include the blog redirect first.

This is currently a temporary (302) redirect. Change the R to R=301 (permanent) only when you are sure it's working OK.

Make sure you browser catch is clear before testing.

Summary

The complete code would look something like:

# Redirect blog subdomain to main domain (required?)
RewriteCond %{HTTP_HOST} ^(www\.)?blog\.example\.com
RewriteRule (.*) https://www.example.com/blog/$1 [R=301,L]

# Non-www to www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) https://www.%{HTTP_HOST}/$1 [R=301,L]

# Non-SSL to SSL
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On    
RewriteBase /

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

It is best not to edit between the # BEGIN WordPress blocks to avoid your custom code being overwritten by WordPress.

Only include the RewriteBase directive once in your code (only the last instance will do anything). Currently, the RewriteBase directive is not actually doing anything anyway, since you have no relative path substitutions.

MrWhite
  • 43,179
  • 8
  • 60
  • 84
  • Thanks a lot for putting this together. You bring up a good point about the proxy. We do have a reverse proxy setup so that we can have a wordpress blog in the subdirectory of a windows server (on a linux server). So I'm not sure if that will interfere. Either way, I made a backup of the .htaccess file and added in your sample. Here's what happens: 1) if I go to a non-http url it redirects me to a 404 page on the windows server 2) if I try with https and non-www it does the same thing. I think you may be on to something with the way the SSL is setup, because I would not have factored that in. – frameworkgeek Jun 19 '17 at 14:50
  • Since you are getting a 404, presumably it's redirecting to the wrong URL? What URL are you requesting, what URL are you being redirected to and what is the desired URL? – MrWhite Jun 19 '17 at 21:14
  • I tried 2 types. 1) http://www.example.com/blog/postname - it redirected to https://www.example.com/notfound.aspx (windows host). The second 2) https://example.com/blog/postname - redirected to https://www.example.com/notfound.aspx @user82217 – frameworkgeek Jun 20 '17 at 14:08