.htaccess htaccess仅将主域重写到子文件夹

ulmd4ohb  于 2022-11-16  发布在  其他
关注(0)|答案(2)|浏览(187)

我使用cPanel帐户托管多个站点。为了有一个干净的public_html,我使用.htaccess将主域重定向到其中一个子文件夹。
因此,当用户键入 * www.example.com * 时,它将被重写为 * www.example.com/mainsite/*,但在URL中仍显示 * www. example. com *。
但是,当使用下面的代码时,每个域(包括子域)都将被重定向到该文件夹:

RewriteCond %{REQUEST_URI} !^/example
RewriteRule ^(.*)$ /mainsite/$1 [NC,L]

如何将www.example.com(和 * www. example. com/files *)作为目标重写到/mainsite?

  • 其他子域应在创建时指向其自己的Document_Root。
frebpwbc

frebpwbc1#

您还需要一个RewriteCond来检查主域:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/mainsite/ [NC]
RewriteRule ^(.*)$ /mainsite/$1 [L]
vktxenjb

vktxenjb2#

这样做,这条线帮助我解决问题。

# .htaccess main domain to subdirectory redirect
    # Do not change this line.
    RewriteEngine on
    # Change example.com to be your main domain.
    RewriteCond %{HTTP_HOST} ^(www.)?example.com$
    # Change 'subdirectory' to be the directory you will use for your main domain.
    RewriteCond %{REQUEST_URI} !^/subdirectory/
    # Don't change the following two lines.
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    # Change 'subdirectory' to be the directory you will use for your main domain.
    RewriteRule ^(.*)$ /subdirectory/$1
    # Change example.com to be your main domain again.
    # Change 'subdirectory' to be the directory you will use for your main domain
    # followed by / then the main file for your site, index.php, index.html, etc.
    RewriteCond %{HTTP_HOST} ^(www.)?example.com$
    RewriteRule ^(/)?$ subdirectory/index.html [L]

相关问题