你不能使用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。
謝謝,我會試試看。這種重定向方法會給我我正在尋找的SEO價值嗎? – livinzlife
@livinzlife'locationProfile.php'腳本重定向到所需的URL格式。然而,**你需要確保當你生成鏈接時,它們看起來就是你想要的**(例如/ locations/)。 –
當我嘗試訪問「www.domain.com/locations/southern-maryland」 – livinzlife