簡體   English   中英

無法使用apache的`mod_rewrite`重定向到子目錄

[英]Trouble redirecting to sub-directory using apache's `mod_rewrite`

我正在嘗試更新舊版Web應用程序,因此我試圖同時設置舊版應用程序和更新版本,然后逐步遷移到新版。
舊應用程序直接位於public_html下,並將新應用程序置於public_html / symsale下 ,現在我希望所有指向該新應用程序的請求都將被重寫,並在symsale之后添加一個Web子目錄,即:像/symsale/path這樣的url ,必須將其重寫為/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重寫規則將所有請求重定向到包含另一個.htaccess和重寫規則的 子目錄Apache RewriteRule 重定向 到子目錄.htaccess重寫將根URL重定向到子目錄

注意 :我使用的是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>

這是一個長鏡頭-mod_rewrite的行為很不幸/古怪,它猜測您是要替換文件系統路徑還是URL路徑。 通過查看替換的第一個組件並查看它是否作為磁盤上的目錄存在(絕對路徑)來進行猜測

您碰巧在文件系統的根目錄下有一個/ symsale /嗎? 我相信[PT]會縮短這種猜測,並強制將結果視為URI。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM