php 通过 .htaccess 更改根文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5792153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing the root folder via .htaccess
提问by Emanuil Rusev
I've got a shared hosting account associated with a domain name and the root folder (correct me if that's the wrong term) is set to /
so that all files on the server are public / accessible through the browser.
我有一个与域名关联的共享主机帐户,并且根文件夹(如果这是错误的术语,请纠正我)设置为/
使服务器上的所有文件都是公开的/可通过浏览器访问。
Can I use .htaccess or something to change the root folder to something like /example.com/public/
?
我可以使用 .htaccess 或其他东西将根文件夹更改为类似的内容/example.com/public/
吗?
回答by clmarquart
If I'm understanding correctly, the following should work
如果我理解正确,以下应该有效
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/ [L,R=301]
This will redirect all requests that do not begin with /public/
to URL that does.
这会将所有不以开头的请求重定向/public/
到以开头的URL。
Hope that helps.
希望有帮助。
回答by acme
The DocumentRoot directivecan't be set in a .htaccess file, only in the server config. As you most likely don't have the privileges to modify the server settings your only solution is to use some rewrite magic as clmarquart already mentioned.
该DocumentRoot指令不能在.htaccess文件中设置,只在服务器配置。由于您很可能没有修改服务器设置的权限,因此您唯一的解决方案是使用clmarquart 已经提到的一些重写魔法。
回答by Tom Claus
This is how i always use it in my framework:
这就是我总是在我的框架中使用它的方式:
Rewritecond %{REQUEST_FILENAME} !-f
RewriteRule (.*) /example.com/public/ [L,NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule !^example.com/public/(.*) /example.com/public/ [L,NC]
回答by Semaj Nekeerv
I use bluehost... this is what works for me: This is helpful when you are on shared hosting, and have multiple domain names.
我使用 bluehost ... 这对我有用:当您使用共享主机并拥有多个域名时,这很有帮助。
Your primary domain is set to public_html but your add-on domains are subfolders inside public_html
您的主域设置为 public_html 但您的附加域是 public_html 中的子文件夹
This makes it so you don't have to have all your primary domain name files be mixed with add-on domain folders... each domain can be in their own folder...
这使得您不必将所有主域名文件与附加域文件夹混合在一起......每个域都可以在自己的文件夹中......
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?PUTYOURDOMAINNAMEHERE.com$
RewriteCond %{REQUEST_URI} !^/PUTYOURFOLDERHERE/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /PUTYOURFOLDERHERE/
RewriteCond %{HTTP_HOST} ^(www.)?PUTYOURDOMAINNAMEHERE.com$
RewriteRule ^(/)?$ PUTYOURFOLDERHERE/ [L]
Options +SymLinksIfOwnerMatch