Apache 通过查询数据库值(如stackoverflow.com URL)重定向

Apache 通过查询数据库值(如stackoverflow.com URL)重定向,apache,.htaccess,mod-rewrite,url-redirection,friendly-url,Apache,.htaccess,Mod Rewrite,Url Redirection,Friendly Url,因此,我使用下面的htaccess代码将URL重定向到干净的URL,但现在我需要做的是将原始URL重定向到新的干净URL 示例 原始URL: example.com/search/store_info.php?store=113&dentist=Dr.%20John%20Doe 清除URL: example.com/search/113/dr-john-doe 我需要的是“原始URL”以重定向到“干净URL”。我之所以需要这样做,是因为这两个URL都出现在谷歌搜索中 我只希望显示干净

因此,我使用下面的htaccess代码将URL重定向到干净的URL,但现在我需要做的是将原始URL重定向到新的干净URL

示例

原始URL:

example.com/search/store_info.php?store=113&dentist=Dr.%20John%20Doe
清除URL:

example.com/search/113/dr-john-doe
我需要的是“原始URL”以重定向到“干净URL”。我之所以需要这样做,是因为这两个URL都出现在谷歌搜索中

我只希望显示干净的URL,只要使用原始URL,它就会自动重定向到干净的URL。它目前没有这样做

这是我的htaccess文件。

ErrorDocument 404 default

<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On    
RewriteBase /search/

RewriteCond %{QUERY_STRING} .
RewriteCond %{THE_REQUEST} /store_info\.php\?store=([a-z0-9]+)&dentist=([^\s&]+) [NC]
RewriteRule ^ %1/%2/? [L,NE,R=301]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_URI} \.(?:jpe?g|gif|bmp|png|ico|tiff|css|js)$ [NC]
RewriteRule ^ - [L]

RewriteRule ^([a-z0-9]+)/([^\s.]*)[.\s]+(.*)$ $1/$2-$3 [NC,DPI,E=DONE:1]

RewriteCond %{ENV:DONE} =1
RewriteRule ^([0-9a-z]+)/([^\s.]+)$ $1/$2 [R=301,NE,L]

RewriteRule ^([a-z0-9]+)/([a-z0-9-]+)/?$ store_info.php?store=$1&dentist=$2 [QSA,L,NC]

</IfModule>
errordocument404默认值
选项-多视图
重新启动发动机
重写库/搜索/
重写cond%{QUERY_STRING}。
重写cond%{THE_REQUEST}/store_info\.php\?store=([a-z0-9]+)&dentor=([^\s&]+)[NC]
重写规则^1/%2/?[L,NE,R=301]
RewriteCond%{REQUEST_FILENAME}-f[或]
RewriteCond%{REQUEST_FILENAME}-d[或]
RewriteCond%{REQUEST_URI}\.(?:jpe?g | gif | bmp | png | ico | tiff | css | js)$[NC]
重写规则^-[L]
重写规则^([a-z0-9]+)/([^\s.]*)[.\s]+(.*)$$1/$2-$3[NC,DPI,E=DONE:1]
RewriteCond%{ENV:DONE}=1
重写规则^([0-9a-z]+)/([^\s.]+)$$1/$2[R=301,NE,L]
重写规则^([a-z0-9]+)/([a-z0-9-]+)/?$store_info.php?store=$1和dentor=$2[QSA,L,NC]

我已经读过RedirectMatch,但是我不知道在我的htaccess文件中实现它的位置

您不需要再使用任意规则/指令来混乱htaccess文件。当前的规则集,与
%{The_REQUEST}
匹配,之后与
%{ENV=DONE}
条件匹配,已经足够了

只需等几天,谷歌就可以再次抓取你的页面,旧的不干净的URL就会消失。你也可以在谷歌网站管理员工具中搜索,看看这是否可以手动触发


PS:您可以从第一条规则中删除
RewriteCond%{QUERY\u STRING}.
行。

存储信息中。php
有如下代码:

<?php
$store = $_GET['store'];

if (empty($store)) {
   header("HTTP/1.1 404 Not Found");
   exit;
}

$dentist = $_GET['dentist']);

// pull dentist from DB by store id. Create this function in PHP and
// query your database
$dentistFromDB = preg_replace('/[\h.]+/', '-', $row['businessName']);

// DB returns an empty or no value
if (empty($dentistFromDB)) {
   header("HTTP/1.1 404 Not Found");
   exit;
}

// DB returned value doesn't match the value in URL
if ($dentistFromDB != $dentist) {
   // redirect with 301 to correct /<store>/<dentist> page
   header ('HTTP/1.1 301 Moved Permanently');
   header('Location: /' . $store . '/' . $dentistFromDB);
   exit;
}

// rest of the PHP code should come now

?>

@anubhava它不会重定向。。因此,如果我转到
example.com/search/store_info.php?store=113
而URL中没有“&dentor=Dr.%20John%20Doe”,它将转到原始URL,而不会转换为干净的URL版本。我希望将其更改为
example.com/search/store\u info.php?store=113
也指向干净的URL。我尝试了重定向301/search/store\u info.php?store=118http://www.example.com/search/118/John-Doe-Dental-Group/
我可以补充一点吗
&dentor=Dr.%20John‌​%20Doe通过上一页的按钮添加到URL字符串中。因此,
example.com/search/‌​store_info.php?store=‌​113
是从URL中获取牙医姓名的原始URL,然后使用htaccess文件创建干净的URL。@anubhava,所以没有类似的方法吗?我知道在web.config文件中有一种方法。但是我不能在htaccess中理解。htaccess文件不能像这样工作吗?我只是想将原始URL插入htaccess文件,然后将其作为301重定向到新的/干净的URL如何将“静态值”添加到现有的htaccess文件中?是否必须为每个URL创建“静态值”,就像为web配置文件创建一样?