匹配.htaccess中的RewriteCond中的基本路径

时间:2019-04-11 07:57:12

标签: apache .htaccess mod-rewrite url-rewriting

我正在使用Apache进行PHP项目。我希望将所有请求重定向到我的根index.php,但在静态资产的特定子目录中除外。这是我的存储库结构:

.
├── .htaccess
├── assets
│   ├── css
│   ├── image
│   └── js
└── index.php

我的.htaccess文件包含以下内容:

RewriteEngine on

# If the requested path is not in the "assets" dir, route through index
RewriteCond %{REQUEST_URI} !^assets/
RewriteRule . index.php [END]

# If the requested path is in the assets dir, but does not exist, route through index
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [END]

当我将项目放在网络根目录时,此方法效果很好。将地图重写为:

  • http://example.com/login-> index.php
  • http://example.com/assets/css/style.css-> assets/css/style.css

当项目安装在子目录(例如test/)中时,此中断,因为REQUEST_URI^/assets不匹配:

  • http://example.com/test/login-> test/index.php
  • http://example.com/test/assets/css/style.css-> test/index.php

似乎每个目录的.htaccess都可以识别目录,但是我找不到如何匹配对^{PROJECT_DIRECTORY}/assets的请求。

我希望通过简单地将存储库放置在服务器上正确的目录中来安装项目。这可能吗?

0 个答案:

没有答案