无法使用apache的`mod_rewrite`重定向到子目录

时间:2015-12-26 08:01:55

标签: php apache .htaccess mod-rewrite redirect

我正在尝试更新旧版Web应用程序,因此我尝试并行设置旧版应用程序和更新版本,然后逐步迁移到新版本。
旧应用程序直接位于 public_html 下,并将新应用程序放在 public_html / symsale 下,现在我希望所有针对新应用程序的请求都被重写并且<在 symsale 之后添加的strong> web 子目录,即:/symsale/path之类的网址必须重写为/symsale/web/path。为此,我创建了一个.htaccess文件并将其放在 symsale 目录下。内容如下:

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteRule ^(.*)(symsale)(.*)$ $1$2/web$3 [L] # i've tried it both with and without the 'L'
</IfModule>

当我尝试导航到/symsale/时,我收到以下错误

  

您无权访问此服务器上的/ symsale /。

但是,当我使用/symsale/web时,它可以正常工作。这有什么问题?

注意:我见过其他一些问题:Apache rewrite rule to redirect all request to subdirectory containing another .htaccess and rewrite rulesApache RewriteRule to subdirectory.htaccess rewrite to redirect root URL to subdirectory

注意 :我正在使用symfony框架(php),因此 web 文件夹中有另一个.htaccess文件,它会重写前控制器的所有路由。 .htaccess的内容如下。

DirectoryIndex app.php

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Determine the RewriteBase automatically and set it as environment variable.
    # If you are using Apache aliases to do mass virtual hosting or installed the
    # project in a subdirectory, the base path will be prepended to allow proper
    # resolution of the app.php file and to redirect to the correct URI. It will
    # work in environments without path prefix as well, providing a safe, one-size
    # fits all solution. But as you do not need it in this case, you can comment
    # the following 2 lines to eliminate the overhead.
    RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
    RewriteRule ^(.*) - [E=BASE:%1]

    # Sets the HTTP_AUTHORIZATION header removed by apache
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect to URI without front controller to prevent duplicate content
    # (with and without `/app.php`). Only do this redirect on the initial
    # rewrite by Apache and not on subsequent cycles. Otherwise we would get an
    # endless redirect loop (request -> rewrite to front controller ->
    # redirect -> request -> ...).
    # So in case you get a "too many redirects" error or you always get redirected
    # to the start page because your Apache does not expose the REDIRECT_STATUS
    # environment variable, you have 2 choices:
    # - disable this feature by commenting the following 2 lines or
    # - use Apache >= 2.3.9 and replace all L flags by END flags and remove the
    #   following RewriteCond (best solution)
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]

    # If the requested filename exists, simply serve it.
    # We only want to let Apache serve files and not directories.
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule .? - [L]

    # Rewrite all other queries to the front controller.
    RewriteRule .? %{ENV:BASE}/app.php [L]
</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 ^/$ /app.php/
        # RedirectTemp cannot be used instead
    </IfModule>
</IfModule>

1 个答案:

答案 0 :(得分:0)

这是一个longshot - mod_rewrite有一个不幸/古怪的行为,它猜测你是在替换文件系统路径还是URL路径。它通过查看替换的第一个组件并查看它是否作为磁盘上的目录(绝对路径)来猜测

你碰巧在文件系统的根目录下有/ symsale /吗?我相信[PT]会使这种猜测短路并强制将结果视为URI。