1

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

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'] 时,它都会说未定义变量。

有人可以请求帮助解决这个神经断裂问题吗?

4

2 回答 2

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]
于 2013-10-08T17:13:20.750 回答
1

这条规则:

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

需要一个$

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

因为它不仅匹配/dir1/dir2

于 2013-10-08T17:14:55.983 回答