0

I have a htaccess file in the root of my public files on my web server:

Header set Content-Security-Policy: upgrade-insecure-requests

DirectoryIndex index.html index.php parking-page.html

RewriteEngine on
# Force www
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# Force SSL
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L,NE]

Options -Indexes

ErrorDocument 404 https://%{HTTP_HOST}/index.php

# for JWT auth
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
# end for JWT auth

I installed a Wordpress blog on /blog , and Wordpress creates a .htaccess file in that folder:

# BEGIN WordPress
# The directives (lines) between `BEGIN WordPress` and `END WordPress` are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>

# END WordPress

I notice that, when I visit http://example.com/blog , it redirects me to https://www.example.com/index.php , while it should go to https://www.example.com/blog . Other pages, for example http://example.com/contact-us, successfully redirects to https://www.example.com/contact-us

Update:

binoculars
  • 2,226
  • 5
  • 33
  • 61

1 Answers1

0

You might have an error in your Rewrite Rule for the redirect from non-www to www. I don't know where you got that code but I've never seen it attempted like that before.

You can replace the separate rules for forcing www and SSL with just the one, as follows:

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,R=302]

Important: Note the use of 302 redirect - you should only use a temporary redirect until you have confirmed it works. Only when you know it is working should you change it to 301.

As you have used a 301 redirect in your current rule, it will be cached and might take a while to clear.

Note that for WP installed in a subfolder, you need to add this Rewrite rule in the .htaccess file in the subfolder.


Note that your Rewrite Rules are not being triggered... it only looks like it works because you have https and www as the site URL in your WP Settings.

Therefore it's possible that the reason http://example.com/blog is going to https://www.example.com/index.php is not because of the rewrites but because the following line in your .htaccess is getting triggered:

ErrorDocument 404 https://%{HTTP_HOST}/index.php

If making the changes above doesn't work, you could look into that as a possible cause.

FluffyKitten
  • 13,824
  • 10
  • 39
  • 52
  • Thanks for your suggestion! Got my code from https://stackoverflow.com/a/24711557. Won't your solution redirect www pages to non-www pages instead of the other way around? Also, should I be concerned about SEO when switching to 302? – binoculars Jun 17 '20 at 21:06
  • @binoculars well spotted :) I took that from the wrong example (I recently answered a question for redirecting to non-www). I've updated the rewrite rule in the question with code taken directly from one of my own websites so I know for a fact its right this time :) As I mentioned in my note, change the 302 to 301 when you have confirmed the redirect is working. Always use 302 to test the redirect, and only when you know it is working should you change it. Testing it with 301 can result in issues with caching if you don't get it right first time. – FluffyKitten Jun 17 '20 at 21:51