最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

htaccess - Redirect main domain to subdirectory

programmeradmin0浏览0评论

Following the following code sample I managed to redirect it, however the site URL on the browser show as domain/subdirectory and not domain.

public_html ROOT .htaccess

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example$
RewriteCond %{REQUEST_URI} !^/subdirectory/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subdirectory/$1
RewriteCond %{HTTP_HOST} ^(www.)?example$
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

Following the following code sample I managed to redirect it, however the site URL on the browser show as domain/subdirectory and not domain.

public_html ROOT .htaccess

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example$
RewriteCond %{REQUEST_URI} !^/subdirectory/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subdirectory/$1
RewriteCond %{HTTP_HOST} ^(www.)?example$
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
Share Improve this question edited Nov 21, 2020 at 16:13 stemon asked Nov 20, 2020 at 16:22 stemonstemon 1551 silver badge8 bronze badges 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 Commented Nov 20, 2020 at 16:34
  • Yes, Yes, Cannot see option to config WP permalinks to omit /subdirectory under settings>permalinks – stemon Commented Nov 21, 2020 at 0:39
  • Settings -> General – MrWhite Commented Nov 21, 2020 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 Commented Nov 21, 2020 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 Commented Nov 21, 2020 at 9:13
 |  Show 1 more comment

1 Answer 1

Reset to default 1

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

  1. If you request example/ (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/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, 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 (no /subdirectory).

发布评论

评论列表(0)

  1. 暂无评论