htaccess重写将无法正常工作

时间:2013-10-08 17:08:30

标签: php regex .htaccess mod-rewrite

我需要一些帮助来重写以下网址:

example.com/news/top/2 /

example.com/news/top /

这是我当前的.htaccess文件

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/([^/\.]+) index.php?view=$1&task=$2
RewriteRule ^news/([^/\.]+)/([0-9]+) index.php?view=news&task=$1&page=$2 [L]
RewriteRule ^news index.php?view=news

我认为这会有效,但每当我访问= example.com/news/top/2/ 我试着回应$ _GET ['page']它说没有定义变量。

能否帮助解决这个神经破裂的问题?

2 个答案:

答案 0 :(得分:2)

确保使用结束锚$

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]+)/?$ index.php?view=news&task=$1&page=$2 [L]
RewriteRule ^([^/.]+)/([^/.]+)/?$ index.php?view=$1&task=$2 [L]
RewriteRule ^news/?$ index.php?view=news [L]

答案 1 :(得分:1)

这条规则:

RewriteRule ^([^/\.]+)/([^/\.]+) index.php?view=$1&task=$2

需要$

RewriteRule ^([^/\.]+)/([^/\.]+)$ index.php?view=$1&task=$2

因为匹配超出/dir1/dir2