php - 使用 htaccess 将 root 重定向到子文件夹而不在 URL 中显示它

标签 php apache .htaccess mod-rewrite shared-hosting

我目前正在使用 Silex framework 托管网站在共享服务器上,我遇到了问题...

Silex 就像 Symfony,在/web/子文件夹中有一个 app.php 文件:然后只能通过 URL website.com/web/访问该网站。我无法创建虚拟主机,因为它是共享服务器,所以我认为解决方案是使用 .htaccess 文件...

我设法自动将 website.com 重定向到 website.com/web/,但我不太喜欢这个选项。我宁愿 website.com 直接指向 website.com/web/但我不知道如何通过使用 .htaccess 文件来做到这一点。几个小时以来,我一直在努力解决这个问题,但它让我很痛苦......

目前我使用这个文件:

<IfModule mod_rewrite.c>
    Options -MultiViews
    
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ web/app.php [QSA,L]
</IfModule>

但它只是将您从 website.com 重定向到 website.com/web

有没有办法让根 url 直接指向带有 .htaccess 文件的/web 文件夹?

非常感谢:)

最佳答案

如果您想访问 http://example.com/subdirectory只需输入 http://example.com这应该有效。

# .htaccess main domain to subdirectory redirect 

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/ [L]

关于php - 使用 htaccess 将 root 重定向到子文件夹而不在 URL 中显示它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19315279/

相关文章:

php - 使用 .htaccess 文件获得好看的 url

php - symfony 寄存器命名空间中的 '\' 和 '\\' 有什么区别?

php - 视频上传 PHP 脚本不工作

c++ - Apache HTTP 服务器 - 如何在模块上打印到控制台?

apache - 为什么我在多个目录中有 .htaccess 文件?

linux - 权限 - Apache 和 Pure-FTPd - 如何设置?

php - 缓存php解析的css、js、txt文件

php - htaccess - 将 php 解析为 html

php - 如何同时使用xampp和mysql服务器

php - 使用 php 读取 .pst 文件的内容