使用 Slim 框架和 htaccess

Working with Slim Framework and htaccess

我有以下代码:

RewriteEngine On


# If you need to use the `RewriteBase` directive, it should be the
# absolute physical path to the directory that contains this htaccess file.
#
# RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f


RewriteRule ^category/(.*)$ searchPage.php?crs_category= [NC,QSA]
RewriteRule ^ index.php [QSA,L]

现在 Slim Framework 的工作方式是将所有请求重定向到 index.php,其中 index.php 将请求的路径路由到其渲染页面。

因此,键入 category/business 不会重写 searchPage.php?crs_category=business 的 url,而是重定向到 index.php,Slim Framework 将抛出一个未找到的页面,因为没有定义要路由的路径 category/:name

我的问题是解决这种情况,我希望除 category/NAME 之外的所有请求都重定向到 index.php

如果有人键入 searchPage.php?crs_category=business 将 url 重写为 category/business,最好使用 301 状态代码,我也将不胜感激。

您可以使用 RewriteCond 将某些条件应用于以下规则。在您的情况下,类似于:

RewriteRule ^category/(.*)$ searchPage.php?crs_category= [L,NC,QSA]
RewriteCond %{REQUEST_URI} !^/searchPage.php
RewriteRule ^ index.php [QSA,L]

我在第一个匹配上添加了"L"开关,所以它不会继续为当前请求解析重写规则。

在下一个请求中,RewriteCond 行显示 "only apply the following rule if the URI does NOT start with searchPage.php"。