1

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
6
  • Is WP installed in this subdirectory? The .htaccess code you posted is presumably in the document root? And you have another "WP" .htaccess file in the subdirectory? Have you also configured WP permalinks to omit /subdirectory from the URL? – MrWhite Nov 20 '20 at 16:34
  • Yes, Yes, Cannot see option to config WP permalinks to omit /subdirectory under settings>permalinks – stemon Nov 21 '20 at 0:39
  • Settings -> General – MrWhite Nov 21 '20 at 0:58
  • Gotcha. It seems to be the way. But, the admin returns 404 and the front end won't load any css/js. – stemon Nov 21 '20 at 1:51
  • What does the .htaccess file contain in the WordPress /subdirectory? Do you have any other directives in the root .htaccess file? – MrWhite Nov 21 '20 at 9:13
0

From the two .htaccess files you posted I can't see how your site is working at all?!

  1. 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

  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).

1
  • It worked on the frontend, but I cannot login to the admin anymore. Have any idea what the issue could be? – stemon Dec 2 '20 at 0:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.