John John - 1 year ago 138
PHP Question

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
, where
route the requested path to its render page.

Hence, typing
will not rewrite the url of
but instead redirect to
where Slim Framework will throw a page not found since there is no path
defined to be routed.

My problem is working out this scenario, where I would like all requests except
to be redirect to

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

Answer Source

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