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

时间:2013-10-11 09:55:18

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

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

Silex就像Synfony,有一个位于/ 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

无论如何我可以让根网址直接指向带有htaccess文件的/ web文件夹吗?

非常感谢:)

2 个答案:

答案 0 :(得分:4)

如果您只想输入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]

答案 1 :(得分:1)

试试这个htaccess配置:

DirectoryIndex web/app.php

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{REQUEST_URI} !^/web/
    RewriteRule (.*) /web/$1
</IfModule>

<IfModule !mod_rewrite.c>
    <IfModule mod_alias.c>
        # When mod_rewrite is not available, we instruct a temporary redirect of
        # the start page to the front controller explicitly so that the website
        # and the generated links can still be used.
        RedirectMatch 302 ^/$ /web/app.php/
        # RedirectTemp cannot be used instead
    </IfModule>
</IfModule>