简体   繁体   English

如何使用.htaccess将root重定向到子目录

[英]How to redirect root to subdirectory using .htaccess

I am currently using shared hosting and have no way to put my files outside the "public_html" folder. 我目前正在使用共享托管,并且无法将我的文件放在“ public_html”文件夹之外。

To work around this, I want to rewrite my "root" site to a subdirectory using the script below in an .htaccess file: 要变通解决此问题,我想使用以下.htaccess文件中的脚本将“根”站点重写为子目录:

RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/subdir/
RewriteRule ^(.*)$ /subdir/$1

Which works great, except when I navigate to www.example.com/subdir/ . 除了我浏览到www.example.com/subdir/之外,这都很好用 That url does not redirect to anywhere, it simply displays the subdirectory. 该url不会重定向到任何地方,它只是显示子目录。 This is a problem for relative urls and such. 这是相对URL之类的问题。

Is there a way to continue rewriting my root to a subdirectory while also rewriting www.example.com/subdir/ to www.example.com ? 有没有办法在将www.example.com/subdir/重写为www.example.com的同时继续将我的根目录重写为子目录?

You can use this rule in root .htaccess: 您可以在根.htaccess中使用此规则:

RewriteEngine On
RewriteBase /

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

And use this rule in /subdir/.htaccess : 并在/subdir/.htaccess使用此规则:

RewriteEngine On
RewriteBase /subdir/

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteCond %{THE_REQUEST} /subdir/(\S*)\s
RewriteRule ^ /%1? [R=302,L,NE]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM