2013-05-20 93 views
0

在我的網站中有很多醜陋的網址,現在我需要將它們重寫爲用戶友好的網址。將醜陋的網址轉換爲用戶友好的網址

這些都是從我的網站醜陋的網址:

http://www.mydomain.com/profiles/tutors/index.php?code=1285&name=Ricky+Pointing 
http://www.mydomain.com/profiles/centers/index.php?code=2285&name=City+&+Gills 
http://www.mydomain.com/about.php 
http://www.mydomain.com/contact.php.... and much more 

所以我要尋找良好的用戶友好型解決方案上面醜陋的URL,這樣的事情:

http://www.mydomain.com/tutors/Ricky_Pointing.html 
http://www.mydomain.com/centers/City_&_Gills.html 
http://www.mydomain.com/about.html 
http://www.mydomain.com/contact.html 

我試過的東西類似於我的.htaccess文件..

RewriteEngine On 

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

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^([0-9]+)/(.+)/$ /profiles/tutors/index.php?code=$1&name=$2 [L,QSA] 

但仍然沒有運氣。 任何人都可以幫助我做到這一點? 謝謝。

+0

你總是可以使用網址縮寫。你可以找到更多的信息從http://stackoverflow.com/questions/742013/how-to-code-a-url-shortener – AliBZ

+0

我知道,我不是故意將它轉換爲像http:// www。 mydomain.com/dgGc32G。我的意思是像http://www.mydomain.com/Ricky_Pointing。你是對的,它可以通過向數據庫添加另一列並存儲縮短的URL來完成。它不需要一個真正的網址縮寫。 – AliBZ

回答

0

我認爲你可能會用RewriteCond s等這樣簡單。你可能有規則是這樣的:

RewriteRule ^about\.html$ about.php [L] 
RewriteRule ^contact\.html$ contact.php [L] 
RewriteRule ^tutors/(.*)\.html$ /profiles/tutors/index.php?name=%1 [L] 
RewriteRule ^centers/(.*)\.html$ /profiles/centers/index.php?name=%1 [L] 

然後您就需要修改你的PHP腳本查找與該名稱適當的事情,而不依賴於具有code存在。它還必須處理原始URL中存在的下劃線。