mod rewrite - How to write .htaccess so that https is on for subpages only but not the home page

admin2025-06-07  47

The root of my domain successfully redirects to a subdirectory, meaning example redirects to example/abc.

example has no WordPress install.

example/abc has its own WordPress install.

The homepage on is not HTTPS and I don't want it to be, however, I do want all the sub-pages to be HTTPS (eg. or )

My current .htaccess file is the standard WordPress stuff...

# BEGIN WordPress

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /abc/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /abc/index.php [L]
</IfModule>

# END WordPress

I've checked and tried many other solutions that I've found here but so far no joy.

p.s. I know to re-save permalinks after making changes like this to the .htaccess file.

The root of my domain successfully redirects to a subdirectory, meaning example redirects to example/abc.

example has no WordPress install.

example/abc has its own WordPress install.

The homepage on http://example/abc is not HTTPS and I don't want it to be, however, I do want all the sub-pages to be HTTPS (eg. https://example/abc/xyz or https://example/abc/thistoo)

My current .htaccess file is the standard WordPress stuff...

# BEGIN WordPress

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /abc/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /abc/index.php [L]
</IfModule>

# END WordPress

I've checked and tried many other solutions that I've found here but so far no joy.

p.s. I know to re-save permalinks after making changes like this to the .htaccess file.

Share Improve this question edited Oct 21, 2018 at 14:27 MrWhite 3,8911 gold badge20 silver badges23 bronze badges asked Oct 21, 2018 at 0:04 au_Martinau_Martin 233 bronze badges 1
  • "example redirects to example/abc" - where are you performing this redirect? "The homepage on http://example/abc" - presumably you mean http://example/abc/ (with a trailing slash)? – MrWhite Commented Oct 21, 2018 at 9:53
Add a comment  | 

1 Answer 1

Reset to default 1

Presumably your .htaccess file is located in the /abc subdirectory? In which case you can do something like the following to force HTTPS on all sub-pages, except the homepage:

This needs to go at the very top of your .htaccess file:

RewriteCond %{HTTPS} !on
RewriteRule !^(index\.php)?$ https://example%{REQUEST_URI} [R=302,L]

This assumes that the SSL cert is installed directly on your application server.

The negated RewriteRule pattern !^(index\.php)?$ only matches non-empty (or not index.php) URL-paths, so this excludes the homepage.

Change the 302 (temporary) to 301 (permanent) only when you are sure it's working OK, to avoid caching issues. Clear your browser cache before testing.


Aside:

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

Since you have set the RewriteBase directive, you can remove the directory-prefix from the RewriteRule substitution (this is what the RewriteBase directive does):

RewriteRule . index.php [L]

However, since your .htaccess file is in the /abc subdirectory, then you don't actually need the RewriteBase directive or the directory-prefix.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749244645a317568.html

最新回复(0)