2013-10-07 42 views
3

我在這裏被困在這裏,我正試圖弄清楚爲什麼它不工作!如何使用htaccess來做到這一點?

這是希望得到的URL看起來像

example.com/news/top/ 

example.com/index.php?view=news&task=top 

,但我需要它這樣說, 「任務」 可以是一些不同勢像

example.com/index.php?view=news&task=newest 
example.com/index.php?view=news&task=help 
example.com/index.php?view=news&task=write 
.... 

current .htaccess的樣子:

DirectoryIndex index.php 
RewriteEngine on 

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

如果我訪問www.example.com/news/ - >它只是如果我訪問www.example.com/news/top/加載的index.php,但沒有通過視圖=新聞

? - >它工作得很好。

但我需要他們兩個的工作,加入了新的生產線爲.htaccess,如:

RewriteRule ^news/ index.php?view=news 

然後參觀www.example.com/news/工作正常,但來訪的www.example.com/news/top /這將不可能獲得任務的價值。

請幫幫我!

預先感謝您。

+0

重寫規則^新聞/頂/的index.php?任務=新聞和任務= $ 1 [L],但這不起作用 – rokkz

+0

看來你去爲RESTful架構。閱讀MVC模式,你會得到一個路由這些請求的好主意。這可能有所幫助:http://www.lornajane.net/posts/2012/building-a-restful-php-server-understanding-the-request – CP510

回答

1

嘗試在你的文檔根目錄添加這些規則的htaccess文件:

RewriteEngine On 

# Redirect access to index.php 
RewriteCond %{THE_REQUEST} \ /index\.php\?view=([^&]+)&task=([^&\ ]+) 
RewriteRule^/%1/%2/? [L,R=301] 

# rewrite to index.php 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?view=$1&task=$2 [L] 

第一重定向像/index.php?view=news&task=foo/news/foo/請求。然後,第二條規則在內部將/news/foo/之類的請求重新寫入/index.php?view=news&task=foo

如果您的所有鏈接已經看起來像/news/foo/,那麼您將不需要第一條規則。

0

我沒有htaccess的老將,但像這可能工作:

RewriteRule /news/(.*) /index.php?view=news&task=$1 [L] 

這應該重寫所有的請求/新聞/ ABC的index.php任務=新聞&視圖= ABC?反向引用$1代表RegEx中由括號包圍的第一個模式。

+0

它一直說它不能從url獲得「任務」。 – rokkz

相關問題