简体   繁体   中英

Working with Slim Framework and htaccess

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.

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 ".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM