如果不满足上述条件,htaccess使用“this.php”

时间:2016-09-08 22:12:46

标签: php apache .htaccess mod-rewrite url-rewriting

我有一个已经存在的php脚本,最近我想集成Slim框架。

我希望达到的目的是使用Slim路由作为最后的手段,如果不满足我已经存在的重写条件,这意味着Slim也将处理404页。

Slim框架.htaccess重写规则是:

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

但是因为我已经有index.php我使用this.php实例化了Slim,所以它应该是

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

问题出现了,我已经预先存在了我想要保留的重写规则,这里有一些重写规则:

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]

因此,如果未满足上述任何模式,我希望他们使用this.php代替。

如果我添加

RewriteRule ^ this.php [QSA,L]

到规则的底部。问题是,在使用它时,上面的每个规则都被忽略,导致所有查询重定向到this.php,这将导致Slim的404错误。如何将RewriteRule ^ this.php [QSA,L]上方的规则设置为首先匹配,然后将其最终传递给this.php之前,如何让htaccess先检查?

希望有人可以提供帮助。

1 个答案:

答案 0 :(得分:1)

通过在底部保持苗条的规则来实现这一点:

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]