Following the following code sample I managed to redirect it, however the site URL on the browser show as domain.com/subdirectory
and not domain.com
.
public_html ROOT .htaccess
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/subdirectory/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subdirectory/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ subdirectory/index.html [L]
subdirectory .htaccess
# 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
# BEGIN LiteSpeed
# The directives (lines) between `BEGIN LiteSpeed` and `END LiteSpeed` are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule Litespeed>
SetEnv noabort 1
</IfModule>
# END LiteSpeed
From the two .htaccess
files you posted I can’t see how your site is working at all?!
-
If you request example.com/
(the document root) then the second rule in the root .htaccess
file rewrites the request directly to /subdirectory/index.html
(not index.php
). If /subdirectory/index.html
exists then the (non-WordPress) file is served, otherwise see #2
-
If you request example.com/foo
(where foo
does not map to a file or directory) then the first rule in the root .htaccess
file internally rewrites the request to /subdirectory/foo
.
At which point the .htaccess
file in the subdirectory rewrites the request back to /index.php
in the document root (not the subdirectory)! This either results in a 404, or a non-WordPress response, or … you’ve have manually created index.php
in the document root to somehow initiate WP?!
How you write these directives depends on whether you have another (non-WordPress) site in the document that still needs to be accessible. However, this would also change how you should configure WordPress as well.
For the sake of this example, I assume you only have the domain example.com
, in which case there is no need to check the Host
header (RewriteCond
directive that checks against HTTP_HOST
).
If you only have the WP site in the subdirectory and are serving no other files from the document root and you wish to completely “hide” the subdirectory then you can write your directives like this:
Root /.htaccess
:
RewriteEngine On
# Unconditionally rewrite everything to the subdirectory
RewriteRule (.*) subdirectory/$1 [L]
The .htaccess
file in the subdirectory (below) prevents a rewrite loop.
/subdirectory/.htaccess
Either remove the RewriteBase
directive altogether and remove the slash prefix on the RewriteRule
substitution string:
# BEGIN WordPress
# :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
Or, (the “WordPress way”), hardcode the /subdirectory
:
# BEGIN WordPress
# :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /subdirectory
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /subdirectory/index.php [L]
</IfModule>
(The RewriteBase
directive is not actually required in the above, but this is often how it is expressed in WordPress tutorials.)
Then, under Settings > General in WordPress, you would set both the “WordPress Address (URL)” (or WP_SITEURL
constant) and the “Site Address (URL)” (or WP_HOME
constant) to the site root https://example.com
(no /subdirectory
).