使用Slim Framework和htaccess [英] Working with Slim Framework and htaccess

查看:107
本文介绍了使用Slim Framework和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=$1 [NC,QSA]
RewriteRule ^ index.php [QSA,L]

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

Now the way Slim Framework works is that all requests are redirected to index.php, where index.php route the requested path to its render page.

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

Hence, typing category/business will not rewrite the url of searchPage.php?crs_category=business but instead redirect to index.php where Slim Framework will throw a page not found since there is no path category/:name defined to be routed.

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

My problem is working out this scenario, where I would like all requests except category/NAME to be redirect to index.php.

当有人键入searchPage.php?crs_category=business将该网址重写为理想情况下具有301状态代码的类别/业务时,我也将不胜感激.

I would also appreciate when someone types searchPage.php?crs_category=business for that url to be rewritten to category/business with ideally a 301 status code.

推荐答案

您可以使用RewriteCond将某些条件应用于以下规则.就您而言,类似:

You can use RewriteCond to apply some conditions to the following rules. In your case, something like:

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

我在第一个匹配项上添加了"L"开关,因此它将不会继续解析当前请求的重写规则.

I added the "L" switch on the first match, so it will not continue parsing rewrite rules for current request.

在下一个请求中,RewriteCond行显示仅在URI不是以 searchPage.php 开头的情况下,才应用以下规则".

On the next request, the RewriteCond line says "only apply the following rule if the URI does NOT start with searchPage.php".

这篇关于使用Slim Framework和htaccess的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆