The Wolf The Wolf - 2 months ago 6
PHP Question

htaccess use "this.php" if the above conditions are not met

I have an already existing php scripts, and recently I wanted to integrate Slim framework.

What I hope to attain is use the Slim routing as the final resort if all my already existing Rewrite Conditions are not met, it means Slim will handle 404 pages too.

Slim framework .htaccess rewrite rule is:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]


but because I already have existing
index.php
I instantiated Slim using
this.php
instead, so it should be

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ this.php [QSA,L]


Here comes the problem, I already have pre-existing rewrite rules that I want to keep, here are some rewrite rules:

Options +FollowSymLinks
RewriteEngine on
RewriteBase /

# Rewrite to www
RewriteCond %{HTTP_HOST} ^example.com [nc]
RewriteRule ^(.*)$ https://www.example.com/$1 [r=301,nc,l]

RewriteRule ^category/([^/]*)/?$ category.php?console=$1 [L,QSA]
RewriteRule ^category/([^/]*)/([^/]*)$ category.php?console=$1&page=$2 [L,QSA]
RewriteRule ^category-amp/([^/]*)/([^/]*)$ category-amp.php?console=$1&page=$2 [L,QSA]
RewriteRule ^games/([^/]*)/?$ games.php?console=$1 [L,QSA]
RewriteRule ^forum^(.*)$ index.php
RewriteRule ^download.php?$ $1/new-page$2 [R=301,L]


so if any of the pattern above was not met, I wish them to use
this.php
instead.

If I add

RewriteRule ^ this.php [QSA,L]


to the bottom of the rules. The problem is, upon using it, every rule above was ignored, cause all queries to redirect to
this.php
, which would throw Slim's 404 errors. How can I make htaccess to check first if the rules set above
RewriteRule ^ this.php [QSA,L]
to match first before passing it finally to
this.php
?

Hope somebody can help.

Answer

Have it this way by keeping slim rule at the bottom:

Options +FollowSymLinks
RewriteEngine on
RewriteBase /

# Rewrite to www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,NE,L]

RewriteRule ^download\.php$ /new-page [R=301,L]

RewriteRule ^category/([^/]*)/?$ category.php?console=$1 [L,QSA]
RewriteRule ^category/([^/]*)/([^/]*)$ category.php?console=$1&page=$2 [L,QSA]
RewriteRule ^category-amp/([^/]*)/([^/]*)$ category-amp.php?console=$1&page=$2 [L,QSA]
RewriteRule ^games/([^/]*)/?$ games.php?console=$1 [L,QSA]
RewriteRule ^forum index.php [L]

# slim rule
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ this.php [L]
Comments