简体   繁体   中英

.htaccess conflict in Wordpress directory

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM