【问题标题】:Working with Slim Framework and htaccess使用 Slim 框架和 htaccess
【发布时间】:2015-08-09 18:40:12
【问题描述】:

我有以下代码:

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=$1 [NC,QSA]
RewriteRule ^ index.php [QSA,L]

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

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

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

如果有人键入 searchPage.php?crs_category=business 以将该 URL 重写为类别/业务,最好是 301 状态代码,我也将不胜感激。

【问题讨论】:

    标签: php apache .htaccess mod-rewrite slim


    【解决方案1】:

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

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

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

    在下一个请求中,RewriteCond 行说“仅当 URI 不以 searchPage.php 开头时才应用以下规则”。

    【讨论】:

    • 谢谢。这也可以通过其他方式完成吗?可以说有人试图访问 searchPage.php?crs_category=Microsoft 它应该重写为 category/microsoft 并让 seo 知道它要使用的格式?谢谢哟
    • 简单的答案是,是的,你可以做你想做的事。不确定“让 seo 知道”部分,但这可能不是问题的一部分,所以我将忽略它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多