2013-10-08 141 views
1

我需要一些幫助改寫以下網址的:htaccesss重寫將無法正常工作

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 [「頁」]它說,變量是沒有定義的。

somone pleas可以幫助解決這個神經斷裂問題嗎?

回答

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

謝謝你這個工作。 – rokkz

+0

@rokkz:很高興知道它爲你工作。 – anubhava

1

這條規則:

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

需要$

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

,因爲它匹配不僅僅是/dir1/dir2