1
votes

I have an issue with a website HTTPS .htaccess redirect.

The website originally has two divisions: the main site (http://example.com/) and a blog site (http://example.com/blog/). I created an .htaccess to enforce HTTPS after the website has been in operation for over 20 years under port 80 (HTTP). So there are many links on multiple websites linking back to this site.

The first issue I had was when I click on a link http://example.com/blog/test it gets redirected to https://example.com/test (note, HTTPS but dropping "blog"). This results in 404 error pages. So I wrote the follwing code:

# Forced HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^ https://example.com%{REQUEST_URI} [NE,L,R]
# rewrite root requests to subfolder
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /blog%{REQUEST_URI} [L]

Now the above code did resolve the 404 error issue on http://example.com/blog/[page_here], but when it gets redirected, it loses the "blog" part. Basically a visitor who clicks on a link http://example.com/blog/test will see https://example.com/test on the address bar. I noticed it did find the correct page and no 404 errors but dropped the "blog" from the address. (I can access blog php pages without "blog" in the URL so it looks like two web addresses for the exact same page).

The other problem is the main site's 404 pages are now directed to the blog site's 404 page and now visitors can access the exact same blog page with or without "blog" in the URL.

Is there anyway to resolve this? The main site is on php while the blog site is on WordPress. The .htaccess file I edited is in the main site's root www folder.

1

1 Answers

0
votes

Have tried with permalink changes?

wp-admin / Settings->Permalinks and set to post name

Working sample code, change required things, but first try with plugin https://wordpress.org/plugins/really-simple-ssl/

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L,NE]

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