2012-09-11 27 views
0

我有一個使用GET變量來決定什麼應該做一個PHP的網站網站...如何使用htaccess刪除醜陋的GET參數?

例如/index.php?s=about&p=2

看來我應該能夠使用重寫規則,所以我可以使用URL來改變這種如:

/about/2 

我該如何得到這種行爲?

回答

1
  1. 您需要將所有鏈接更改爲/about/2
  2. 您需要調整任何相對鏈接是絕對鏈接或將其添加到您的所有網頁的標題:

    <base href="/"> 
    
  3. 添加這些規則的htaccess文件在你的文檔根目錄更改不錯尋找鏈接回醜鏈接:

    RewriteEngine On 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^([^/]+)/([^/]+)/? /index.php?s=$1&p=$2 [L] 
    
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^([^/]+)/? /index.php?s=$1 [L] 
    
  4. 只是讓搜索引擎可以重新索引你的網頁,你需要301加入此相同的htaccess文件重定向舊醜陋的人到好看的:

    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?s=([^&]+)&p=([^&]+) 
    RewriteRule ^index.php$ /%1/%2 [L,R=301] 
    
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?s=([^&]+) 
    RewriteRule ^index.php$ /%1 [L,R=301] 
    
+0

完美答案,非常感謝。 – DaFoot