2014-01-08 35 views
0

我現在有這樣我的網站鏈接:我可以這樣改變這個東西URL重寫不知道網頁標題

http://www.domain.com/locations/locationProfile.php?locationID=22 

我希望,對於SEO的目的:

http://www.domain.com/locations/southern-maryland 

「南馬里蘭州」目前是從MySQL數據庫中作爲位置ID 22的位置名稱提取的。當我的網站結構當前使用較不吸引人的第一個版本時,是否甚至可以將它放入URL中?

回答

0

你不能使用htaccess爲你做這個。您可以使用可在服務器配置中定義的RewriteMap(無法在htaccess文件中定義它),但如果您只在locationProfile.php腳本中執行此操作,則該操作將變得容易得多。

如果該請求與使用REQUEST_URI值的查詢字符串讓你可以檢測:

if ($_SERVER['REQUEST_URI'] == '/locations/locationProfile.php' && 
    isset($_GET['locationID'])) { 
    // go into DB and extract the location name then redirect 
    header('Location: /locations/' . $location-name); 
} 
在htaccess文件

然後在你的文檔根目錄):

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^locations/([^/]+)$ /locations/locationProfile.php?locationName=$1 [L,QSA] 

最後,使你的php腳本查找locationName參數,並根據該參數服務器而不是ID。

+0

謝謝,我會試試看。這種重定向方法會給我我正在尋找的SEO價值嗎? – livinzlife

+0

@livinzlife'locationProfile.php'腳本重定向到所需的URL格式。然而,**你需要確保當你生成鏈接時,它們看起來就是你想要的**(例如/ locations/)。 –

+0

當我嘗試訪問「www.domain.com/locations/southern-maryland」 – livinzlife