Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.htaccess 在不知道页面标题的情况下重写URL_.htaccess_Rewrite - Fatal编程技术网

.htaccess 在不知道页面标题的情况下重写URL

.htaccess 在不知道页面标题的情况下重写URL,.htaccess,rewrite,.htaccess,Rewrite,我目前在我的网站上有如下链接: http://www.domain.com/locations/locationProfile.php?locationID=22 http://www.domain.com/locations/southern-maryland 我希望,出于SEO目的,我可以将此更改为以下内容: http://www.domain.com/locations/locationProfile.php?locationID=22 http://www.domain.com/l

我目前在我的网站上有如下链接:

http://www.domain.com/locations/locationProfile.php?locationID=22
http://www.domain.com/locations/southern-maryland
我希望,出于SEO目的,我可以将此更改为以下内容:

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

“Southern Maryland”当前来自mysql数据库,作为位置ID 22的位置名。当我的网站结构目前使用不太吸引人的第一个版本时,是否有可能将其输入URL?

您不能使用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文件中):


最后,让您的php脚本查找
locationName
参数,并基于该参数而不是ID为服务器提供正确的页面。

谢谢,我将尝试一下。这个重定向方法会给我我正在寻找的SEO值吗?@livinzlife将
locationProfile.php
script重定向到您想要的URL格式。但是,您需要确保在生成链接时,它们的外观符合您的要求(例如/locations/)