Working with Slim Framework and htaccess

▼魔方 西西 提交于 2019-12-13 07:40:08

问题


I have the following code:

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]

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.

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.

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

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.


回答1:


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]

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

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



来源:https://stackoverflow.com/questions/31907600/working-with-slim-framework-and-htaccess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!