【发布时间】:2015-02-09 11:18:54
【问题描述】:
我在 htaccess 中有一个 oldPage.php 到 newPage.php 的 301 重定向:
RedirectMatch 301 ^/oldPage.php$ /newPage.php
现在我希望将 forceOldPage.php 重定向到 oldPage.php(不重定向到 newPage.php)
有可能吗?我该怎么做?
【问题讨论】:
我在 htaccess 中有一个 oldPage.php 到 newPage.php 的 301 重定向:
RedirectMatch 301 ^/oldPage.php$ /newPage.php
现在我希望将 forceOldPage.php 重定向到 oldPage.php(不重定向到 newPage.php)
有可能吗?我该怎么做?
【问题讨论】:
首先删除您的.htaccess 重定向,如果引用者不是forceOldPage.php,则改为在oldPage.php 中重定向。
oldPage.php 顶部的类似内容是一个好的开始,根据需要进行调整和完善。
<?php
// If the referring page isn't "forceOldPage.php"...
if ( !strpos( $_SERVER['HTTP_REFERER'], 'forceOldPage.php' ) ) {
// ...301 redirect to "newPage.php"
header( "HTTP/1.1 301 Moved Permanently" );
header( "Location: /newPage.php" );
die();
}
?>
【讨论】:
一种可能性是假设“强制”(选择任何字符串)不会成为任何转到 oldPage.php 的页面上的查询字符串的一部分。使用 RewriteCond 和 RewriteRule。然后将 forceOldPage.php 重定向到 oldPage.php?force。
RewriteEngine on
RewriteCond %{QUERY_STRING} !force
RewriteRule oldPage.php newPage.php [R=301]
RedirectMatch 301 ^/forceOldPage.php$ /oldPage.php?force
仅供参考:我尝试使用 HTTP_REFERER 来检测用户是否来自 forceOldPage。但是,引用者不会在重定向时传递到新页面(至少在我的环境中)。
【讨论】: