1

我的 mod_rewrite 没有遇到另一个问题,首先,时间是我的 .htaccess 文件:

Options +FollowSymlinks
RewriteEngine on

RewriteRule ^news/([0-9]+)/?$ /news?id=$1 [L,QSA]
RewriteRule ^contact/([0-9]+)/?$ /contact?do=$1 [L,QSA]
RewriteRule ^account/([a-zA-Z]+)/?$ /account?action=$1 [L,QSA]
RewriteRule ^admin/([a-zA-Z]+)/?$ /admin?action=$1 [L,QSA]
RewriteRule ^login/([a-zA-Z]+)/?$ /login?action=$1 [L,QSA]
RewriteRule ^register/([a-zA-Z]+)/?$ /register?action=$1 [L,QSA]


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L] <- This line doesn't work
RewriteRule ^([\w\d~%.:_\-]+)$ index.php?page=$1 [L,QSA]

除了RewriteRule ^([^.]+)$ $1.php [NC,L]之外,所有行都正常工作, 当我使用此行时,所有其他规则都会中断。

我究竟做错了什么?

4

2 回答 2

2

您需要L在有问题的规则中使用标志。

对于删除 .php 扩展名,这是更好的规则:

Options +FollowSymlinks -MultiViews

RewriteEngine on

## If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
## If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
## don't do anything
RewriteRule ^ - [L]

RewriteRule ^news/([0-9]+)/?$ /news?id=$1 [L,QSA]
RewriteRule ^contact/([0-9]+)/?$ /contact?do=$1 [L,QSA]
RewriteRule ^account/([a-zA-Z]+)/?$ /account?action=$1 [L,QSA]
RewriteRule ^admin/([a-zA-Z]+)/?$ /admin?action=$1 [L,QSA]
RewriteRule ^login/([a-zA-Z]+)/?$ /login?action=$1 [L,QSA]
RewriteRule ^register/([a-zA-Z]+)/?$ /register?action=$1 [L,QSA]

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

RewriteRule ^(.+?)/?$ index.php?page=$1 [L,QSA]
于 2013-11-08T18:49:37.013 回答
1

尝试类似的东西

#Preventing loop 
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]

RewriteCond %{REQUEST_URI} !^.+\.php.*$
RewriteRule .* - [L]

把这个放在后面RewriteRule ^register/([a-zA-Z]+)/?$ /register?action=$1 [L,QSA]

于 2013-11-08T18:46:21.780 回答