2016-10-03 101 views
1

我需要用標題和ID動態重寫URL。我想讓網址動態代替用標題和ID重寫URL動態

http://dev.example.com/beweb/newsdetails.php?id=8&title=iphone 

http://dev.example.com/beweb/iphone-8.html 

當我運行上面的網址,以便它dyanamically做什麼,我需要..但得到錯誤

" Not Found 
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request. 
The requested URL /beweb/iphone-8.html was not found on this server." 

我有使用以下代碼:

RewriteEngine On 
RewriteCond %{THE_REQUEST} /beweb/newsdetails.php\?id=([^&\s]+)&title=([^&\s]+) [NC] 
RewriteRule^/beweb/%2-%1\.html? [NC,R,L,NE] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^%2-([^.]+)\.html$ /beweb/newsdetails.php?id=$1&title=$2 [QSA,L,NC] 

請幫幫我。

回答

0

規則幾乎是正確的。你只是錯誤配置的最後一條語句,這應該是:

RewriteRule ^([^-]+)-([^.]+)\.html$ /beweb/newsdetails.php?id=$2&title=$1 [QSA,L,NC] 
+0

我已經把代碼和它的工作..謝謝:)。但其他問題的產生,因爲我想從URL(獲取參數)ID ..現在得到錯誤..你能幫我。 – Jeff

+0

@Jeff只需交換上面的$ 1和$ 2即可。 – hjpotter92

+0

我需要幫助你..你能做到嗎 – Jeff

0

隨着這些線路可以檢查是不是在服務器上,然後如果negetive一個真正的靜態文件或目錄的路徑,proccess文件名,以滿足您用戶:

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^beweb/([a-z]+)\-([0-9]+).html$ /beweb/index.php?id=$2&title=$1 [QSA,L,NC] 

QSA是指查詢字符串添加到新的URL

+0

僅有代碼的答案通常是不夠的。添加一些解釋*爲什麼*你的代碼工作,爲什麼OP的代碼沒有。否則,你的答案只能幫助那些只是複製粘貼你的答案的OP--而這不是你想要使用的方式。 –

+0

@ Al.G。是的,你是對的,我很快就會加入解釋。 –