通过查询stackoverflow.com URL

时间:2016-08-15 19:04:03

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

所以我使用以下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 ”。我需要这样做的原因是两个网址都出现在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文件中。

1 个答案:

答案 0 :(得分: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

?>