【问题标题】:Redirection by querying database value like stackoverflow.com URLs通过查询数据库值(如 stackoverflow.com URL)进行重定向
【发布时间】:2016-12-22 00:41:07
【问题描述】:

所以我使用以下 htaccess 代码将 URL 重定向到干净的 URL,但是,现在我需要做的是让原始 URL 重定向到新的干净 URL。

示例

原网址:

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

干净的网址:

example.com/search/113/dr-john-doe

我需要的是“ORIGINAL URL”来重定向到“CLEAN URL”。我需要这样做的原因是这两个 URL 都出现在 Google 搜索中。

我只希望显示干净的 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>

我已经阅读了 RedirectMatch,但是我不知道在哪里将它实现到我的 htaccess 文件中。

【问题讨论】:

  • @anubhava 它不会重定向.. 因此,如果我转到example.com/search/store_info.php?store=113 而 URL 中没有“&dentist=Dr.%20John%20Doe”,它会转到原始 URL 并且不会转换到干净的 URL 版本。我希望将其更改为 example.com/search/store_info.php?store=113 也转到干净 URL 的位置。我试过Redirect 301 /search/store_info.php?store=118 http://www.example.com/search/118/John-Doe-Dental-Group/
  • 我可以补充一点,&amp;dentist=Dr.%20John‌​%20Doe 通过上一页的按钮添加到 URL 字符串。所以example.com/search/‌​store_info.php?store=‌​113 是从 URL 中获取牙医姓名的原始 URL,然后使用 htaccess 文件创建干净的 URL。
  • @anubhava 所以没有办法像this 那样做吗?我知道 web.config 文件中有一种方法。但我无法在 htaccess 中弄清楚。
  • 不能像this这样的htaccess文件功能吗?我只是想将原始 URL 插入到 htaccess 文件中,然后将其作为 301 重定向到 NEW/clean URL
  • 如何将“静态值”添加到我现有的 htaccess 文件中?是否必须像为 Web 配置文件一样为每个 URL 创建“静态值”?

标签: apache .htaccess mod-rewrite url-redirection friendly-url


【解决方案1】:

在你的store_info.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

?>

【讨论】:

    【解决方案2】:

    您不再需要使用任意规则/指令来弄乱您的 htaccess 文件。当前的规则集,与%{THE_REQUEST} 匹配,后来与%{ENV=DONE} 条件匹配,绰绰有余。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-19
      • 1970-01-01
      • 1970-01-01
      • 2015-04-28
      • 1970-01-01
      • 1970-01-01
      • 2021-07-26
      相关资源
      最近更新 更多