使用正则表达式自定义重定向?

时间:2013-05-29 16:20:42

标签: apache .htaccess redirect

我想重定向以下网址结构:

/vehicle_details.php?veh_id=918

要:

http://www.example.co.uk/my/url

veh_id是任何东西时,我需要它,所以它会重定向所有。

2 个答案:

答案 0 :(得分:0)

感谢@naomi的想法。

由于vehicle_details.php是一个非常旧的文件,我将简单地创建一个新文件:

<?php 
    // old website redirects
    header("Location: http://www.example.com/my/url/");
    exit;
?>

答案 1 :(得分:0)

通过httpd.conf启用mod_rewrite和.htaccess,然后将此代码放在.htaccess目录下的DOCUMENT_ROOT中:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+vehicle_details\.php\?veh_id=[^\s]+ [NC]
RewriteRule ^ http://www.example.co.uk/my/url? [R=302,L]

验证一切正常后,将R=302替换为R=301。在测试mod_rewrite规则时,请避免使用R=301(永久重定向)。