2012-07-22 23 views
2

我想縮短這個URL:'http:// localhost/school/readmore?id = 2'(必須在引號中,因爲SO不允許發佈本地主機URL) 。我已經讓這樣的「http://localhost/school/readmore-2.html」這個.htaccess文件:使用htaccess創建一個美麗的URL

RewriteEngine On 
IndexIgnore * 

RewriteCond %{SCRIPT_FILENAME} !-d 
RewriteCond %{SCRIPT_FILENAME} !-f 

RewriteRule ^readmore-([1-9]+).html$ readmore.php?id=$1 

,這裏是修改後的鏈接

<a href="readmore-<?php echo $show_row['id']; ?>.html">Readmore</a> 

但我想要的URL看起來像這樣'http:// localhost/school/readmore/2'。 一旦我嘗試過,但它完全失敗;這裏是沒有工作的腳本:

RewriteEngine On 
IndexIgnore * 

RewriteCond %{SCRIPT_FILENAME} !-d 
RewriteCond %{SCRIPT_FILENAME} !-f 

RewriteRule ^readmore/([1-9]+).html$ readmore.php?id=$1 

<a href="readmore/<?php echo $show_row['id']; ?>.html">Readmore</a> 

起初我以爲它的工作,但CSS腳本沒有加載,因爲我改變了「-」到這個「/」的鏈接和腳本讀取它作爲夾。

+0

確定'^ readmore /([1-9] +).html $'不會影響你的CSS文件,當你指定一個'.html'後綴 – Hubro 2012-07-22 01:43:54

回答

1

讓您遠離.

RewriteRule ^readmore/([1-9]+)\.html$ readmore.php?id=$1 
+0

你好...我想問你例如:'http:// localhost/school/galery'改爲'http:// localhost/school/readmore/galery' – messerchainey 2012-07-22 03:34:35

+0

這不應該發生。你只是匹配並重寫那些以readmore開頭並以.html結尾的文件。 – 2012-07-22 03:38:48

+0

這裏是我的鏈接代碼Readmore ...我做錯了什麼? – messerchainey 2012-07-22 03:46:37

0

您的瀏覽器認爲它是一個子目錄,所以它引用到的 'http://localhost/school/readmore/style.css' 而不是「HTTP://本地主機/school/style.css'

爲了解決這個問題,你應該使用絕對路徑樣式表和鏈接,如

<link rel="stylesheet" href="http://localhost/school/style.css" /> 

<link rel="stylesheet" href="/school/style.css" /> 
+0

謝謝gronke :) – messerchainey 2012-09-19 14:08:03