2015-02-06 54 views
0

嗨,我正在做一個網站,我希望友好和乾淨的鏈接。我試圖使一個get參數看起來更清潔的鏈接。這裏的鏈接是如何在目前的樣本鏈接:從URL中刪除index.php和參數名稱

http://www.example.com/index.php?item=cd-player 

這裏是我想要的鏈接查看:

http://www.example.com/cd-player 

我已經能夠擺脫掉的index.php url只留下參數,但我需要幫助擺脫「?item」位。

這裏是我到目前爲止已經試過:

RewriteRule ^index\.php(.*)$ /$1 [R,L,QSA] 
RewriteRule ^(.+?)/?$ /?item=$1 [L,QSA] 

而且,這可能是一個PHP代碼,而不是用於.htaccess文件代碼,但如果用戶輸入http://www.example.com/cd-player,怎麼會知道「cd-player」是一個名爲「item」的get變量的值?

編輯:我與下面的答案遇到的問題是,我使用下面的代碼刪除文件擴展名,並在下面的答案代碼把文件作爲查詢:

RewriteRule ^([^\.]+)$ $1.php [NC,L] 

回答

0

您需要像這樣做。這將讓你有乾淨的鏈接

RewriteEngine On 

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^([^/.]+)$ $1.php [NC,L] 

#This means if its not a real file 
RewriteCond %{REQUEST_FILENAME} !-f 
#This means if its not a real directory 
RewriteCond %{REQUEST_FILENAME} !-d 
#Then take the capture and rewrite it 
RewriteRule ^(.+)$ /index.php?item=$1 [L] 
+0

你可以,如果你想刪除的PHP添加此延期。 – 2015-02-06 16:23:43

0

試試這個:

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ /index.php?item=$1 [L] 
0

您需要2條規則實際上是:

RewriteEngine On 

# external redirect from actual URL to pretty one 
RewriteCond %{THE_REQUEST} /index\.php\?item=([^\s&]+) [NC] 
RewriteRule^/%1? [R=302,L,NE] 

# add .php extension 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f [NC] 
RewriteRule ^(.+?)/?$ /$1.php [L] 

# internal forward from pretty URL to actual one 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.+)$ /index.php?item=$1 [L,QSA] 
+0

我的問題是,現在正在將同一文件夾中的其他文件視爲查詢,因爲我已從所有文件中刪除文件擴展名,請參閱編輯 – Matt9Atkins 2015-02-06 16:15:53

+0

您需要比'RewriteRule ^([^ \。] +)$ $ 1更強的規則。 php [NC,L]'添加'.php'擴展名(請參閱編輯) – anubhava 2015-02-06 16:19:09