通过 .htaccess 更改根文件夹

发布于 2024-11-03 04:18:04 字数 172 浏览 1 评论 0原文

我有一个与域名关联的共享托管帐户,并且根文件夹(如果这是错误的术语,请纠正我)设置为 / ,以便服务器上的所有文件都是公开的/可以通过浏览器。

我可以使用 .htaccess 或其他内容将根文件夹更改为 /example.com/public/ 之类的文件夹吗?

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/?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

鲜肉鲜肉永远不皱 2024-11-10 04:18:05

无法在 .htaccess 中设置 DocumentRoot 指令文件,仅在服务器配置中。由于您很可能没有修改服务器设置的权限,因此您唯一的解决方案是使用一些重写魔法,如 clmarquart 已经提到了

The DocumentRoot directive can'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.

青瓷清茶倾城歌 2024-11-10 04:18:05

我使用 bluehost...这对我有用:
当您使用共享主机并拥有多个域名时,这非常有用。

您的主域设置为 public_html 但您的附加域是 public_html 内的子文件夹

这使得您不必将所有主域名文件与附加域文件夹混合...每个域都可以位于他们自己的文件夹...

RewriteEngine on 
RewriteCond %{HTTP_HOST} ^(www.)?PUTYOURDOMAINNAMEHERE.com$ 
RewriteCond %{REQUEST_URI} !^/PUTYOURFOLDERHERE/ 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ /PUTYOURFOLDERHERE/$1  
RewriteCond %{HTTP_HOST} ^(www.)?PUTYOURDOMAINNAMEHERE.com$ 
RewriteRule ^(/)?$ PUTYOURFOLDERHERE/ [L]
Options +SymLinksIfOwnerMatch

I use bluehost... this is what works for me:
This is helpful when you are on shared hosting, and have multiple domain names.

Your primary domain is set to public_html but your add-on domains are subfolders inside 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/$1  
RewriteCond %{HTTP_HOST} ^(www.)?PUTYOURDOMAINNAMEHERE.com$ 
RewriteRule ^(/)?$ PUTYOURFOLDERHERE/ [L]
Options +SymLinksIfOwnerMatch
网名女生简单气质 2024-11-10 04:18:05

我用这个:

# .htaccess main domain to subdirectory redirect


RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteCond %{REQUEST_URI} !example/public/
RewriteRule (.*) /example/public/$1 [L]

I use this:

# .htaccess main domain to subdirectory redirect


RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteCond %{REQUEST_URI} !example/public/
RewriteRule (.*) /example/public/$1 [L]
清醇 2024-11-10 04:18:04

如果我理解正确,以下内容应该有效。

RewriteEngine On

RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1 [L,R=301]

这会将所有不以 /public/ 开头的请求重定向到以 /public/ 开头的 URL。

希望有帮助。

If I'm understanding correctly, the following should work

RewriteEngine On

RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1 [L,R=301]

This will redirect all requests that do not begin with /public/ to URL that does.

Hope that helps.

失与倦" 2024-11-10 04:18:04

这就是我总是在我的框架中使用它的方式:

Rewritecond %{REQUEST_FILENAME} !-f
RewriteRule (.*) /example.com/public/$1 [L,NC]  

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

This is how i always use it in my framework:

Rewritecond %{REQUEST_FILENAME} !-f
RewriteRule (.*) /example.com/public/$1 [L,NC]  

RewriteCond %{REQUEST_URI}  ^/$
RewriteRule !^example.com/public/(.*) /example.com/public/$1 [L,NC]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文