2

我有一个具有以下目录结构的简单本地站点:

/
    test2/
        .htaccess
        bootstrap.php
        git-service/
            bootstrap.php

我想要完成的是,当用户请求 www.example.com/test2/git/ 时,他会获取 git-service 目录的内容,而如果他尝试直接请求目录 git-service (www.example .com/test2/git-service/),该请求被视为正常请求(例如作为主 test2/bootstrap.php 的 GET 值给出)。

我现在所做的是:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /test2

RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteCond %{REQUEST_URI} ^/test2/git($|/.*$)
RewriteRule . /test2/git-service/bootstrap.php?request=GIT [L]

RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteCond %{REQUEST_URI} ^/test2/git-service($|/.*$)
RewriteRule . /test2/bootstrap.php [L]

问题是 /test2/git 和 /test2/git-service 请求都被传递到主 bootstrap.php 文件 (test2/bootstrap.php)。如果我注释掉第二个块(处理 git-service 请求的块),则行为是正确的,这意味着 test2/git 请求被正确传递给 git-service/bootstrap.php 文件。但是当我激活第二部分时,对 test2/git/ 的相同请求现在被传递到主 test2/bootstrap.php 文件。

另一方面,所有这一切背后的原因是我试图将目录的实际名称与获取它的请求及其内容分离。

非常感谢您提前。

====编辑====

非常感谢你,anubhava,你的建议效果很好,尽管需要做一些小改动,因为服务器在请求 git/ 页面时不断给我 404 错误(git-service 页面被正确映射)。

对我有用的代码是这样的:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /test2/

RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
#RewriteRule ^git(/.*|)$ git-service/bootstrap.php?request=GIT [L,NC]
RewriteCond %{REQUEST_URI} ^/test2/git(|/.*)$
RewriteRule . /test2/git_service/bootstrap.php?request=GIT [L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^git-service(|/.*)$ bootstrap.php [L,NC]

由于某种原因,我无法删除第二个 RewriteCond 行,也无法将第一个 RewriteRule 中的全匹配点替换为实际条件,否则会出现 404 错误。

4

1 回答 1

2

有这样的代码:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /test2/

RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^git(/.*|)$ git-service/bootstrap.php?request=GIT [L,NC]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^git-service(/.*|)$ bootstrap.php [L,NC]
于 2013-09-12T13:08:30.973 回答