2014-03-30 52 views
1

好的,所以我已經看了很長一段時間了這個話題,可能因爲我仍然很難理解它 - 所以我回去了以最簡單的方式提出基本要求。用.htaccess重寫網址

我有一個空的.htaccess文件 我有http://www.website.co.uk/news.php?id=111111 當前URL我希望這成爲http://www.website.co.uk/news/111111

我如何做到這一點?

也請不,雖然這是現在的URL,我打算做一些更改網站,網址在未來可能是:

http://www.website.co.uk/news.php?city=city&issue=1&title=the-title&id=111111

http://www.website.co.uk/news/city/issue/the-title/111111

如何我可以做到這一點,以便未來的變化也能工作嗎?到目前爲止,我有:

RewriteEngine On 
RewriteRule ^news/(.+)$ news.php?id=$1 [L] 

這仍然顯示完整的url和在news/111111重定向到錯誤頁面中鍵入。請幫忙!

+1

可能的重複[參考:mod \ _rewrite,URL重寫和「漂亮鏈接」解釋](http://stackoverflow.com/questions/20563772/reference-mod-rewrite-url-rewriting-and-pretty-links - 解釋) – deceze

+0

@deceze你能回答[這](http://webmasters.stackexchange.com/questions/59543/apache-url-rewriting-not-working)的問題? –

+0

@deceze你能解釋一下我的例子嗎?我已經嘗試了幾個教程/解釋的博客,但似乎沒有工作 – user3177012

回答

0

添加以下到您的htaccess應該工作:

RewriteEngine On 
RewriteCond %{QUERY_STRING} ^id=(.*)$ 
RewriteRule ^news.php$ /news/%1? [L,R] 

RewriteRule ^news/(.*) news.php?id=$1 [QSA] 

以上將改變http://www.website.co.uk/news.php?id=111111http://www.website.co.uk/news/111111

和下方將改變

RewriteCond %{QUERY_STRING} ^city=(.*)&issue=(.*)&title=(.*)&id=(.*)$ 
RewriteRule ^news.php$ /news/%1/%2/%3/%4 [L,R] 

RewriteRule ^news/(.*)/(.*)/(.*)/(.*) news.php?city=$1&issue=$2&title=$3&id=$4 [QSA] 

http://www.website.co.uk/news.php?city=city&issue=1&title=the-title&id=111111http://www.website.co.uk/news/city/issue/the-title/111111

%1,%2,$3,%4中的值取自city=,issue=後的參數。 title=id=

city=LondonLondon將包含在%1

第二重寫規則將讓你找到id使用。

+0

Thnak你!這似乎在某種程度上工作,但是,我被重定向到一個錯誤頁面裏面的新聞 - 我認爲由於我有一個$ id = $ _GET [「id」];聲明。我怎樣才能解決這個問題? – user3177012

+0

這適用於重定向,但它重定向到錯誤頁面,因爲我有'if(isset($ _ GET [「id」])){ $ id = $ _GET [「id」]; } else { header(「Location:redirect.php?type = error」);' - 我如何確保它能夠識別ID? – user3177012

+0

對不起,試試我的編輯。我添加了一行。 – Howli