如何从.htaccess重定向(rewriteRule)中排除.php文件?

时间:2014-01-16 23:42:05

标签: php apache .htaccess mod-rewrite redirect

我想知道如何排除特定的.php文件重定向重写规则。

site.com/images/resizer.php一直重定向到site.com/images/resizer/,这是一个问题因为我有一些标题设置如下:site.com/images/resizer/?page=2

在我的.htaccess中,我有两个重写规则,删除.php文件扩展名并在网址末尾强制斜杠,我想排除resizer.php被重写。

#if page with .php is requested then remove the extension
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1/ [R=302,L,NE]

#Force a trailing slash to be added
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s/+([^.]+?[^/.])[\s?] [NC]
RewriteRule ^ /%1/ [R=302,L]

#silently rewrite to webroot
RewriteCond %{REQUEST_URI} !/webroot/ [NC]
RewriteRule ^ /webroot%{REQUEST_URI} [L]

#.php ext hiding
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

我希望resizer如下:site.com/images/resizer.php?page=2

我尝试在斜杠和.php隐藏扩展规则下添加以下内容,但它不起作用,我完全坚持这个..

尝试失败(在rewriteRule下)

RewriteCond %{REQUEST_URI} !^/images/resizer$ [NC]

RewriteRule ^images/resizer/?$ /images/resizer.php [NC,L]

1 个答案:

答案 0 :(得分:2)

你很亲密。在两个重定向规则中,添加条件:

RewriteCond %{REQUEST_URI} !^/images/resizer\.php

所以:

#if page with .php is requested then remove the extension
RewriteCond %{REQUEST_URI} !^/images/resizer\.php
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1/ [R=302,L,NE]

#Force a trailing slash to be added
RewriteCond %{REQUEST_URI} !^/images/resizer\.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s/+([^.]+?[^/.])[\s?] [NC]
RewriteRule ^ /%1/ [R=302,L]