doubu5154 2015-12-26 08:01
浏览 113

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

I am trying to update a legacy web application, so I'm trying to set up both the legacy application and the updated version in parallel, and gradually migrate to the new one.
The old application is located directly under public_html, and have put the new Application under public_html/symsale, now I want all requests directed to the new application to be rewritten and a web subdirectory added after symsale, i.e: a url like /symsale/path, must be rewritten to /symsale/web/path. for that I've create a .htaccess file and put it under the symsale directory. The contents follow:

<IfModule mod_rewrite.c>
    RewriteEngine On

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

When I try to navigate to /symsale/ I get the following error

You don't have permission to access /symsale/ on this server.

However when I use /symsale/web instead, it works just fine. What's the problem here?

Note: I have seen these other questions: Apache rewrite rule to redirect all request to subdirectory containing another .htaccess and rewrite rules, Apache RewriteRule to subdirectory, .htaccess rewrite to redirect root URL to subdirectory

Note: I'm using the symfony framework (php) so there is another .htaccess file inside the web folder, which rewrites all routes to the front controller. The contents of that .htaccess follow.

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条回答 默认 最新

  • duanpin2009 2015-12-27 16:50
    关注

    Here is one longshot -- mod_rewrite has an unfortunate/quirky behavior where it guesses whether you're substituting a filesystem path or a URL path. It guesses by looking at the first component of the substitution and seeing if it exists as a directory on disk (absolute path)

    Do you happen to have a /symsale/ at the root of your filesystem? I believe [PT] will short-circuit this guessing and force the result to be treated as a URI.

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?