【问题标题】:Htaccess and PHP redirect, page not foundHtaccess 和 PHP 重定向,找不到页面
【发布时间】:2014-06-26 13:06:00
【问题描述】:

我的重定向有点问题,使用 PHP 我做了一个 301 重定向到这个 URL(变量来自搜索表单):

ddddd.com/ads-CATEGORY-CITY/?var1=1&var2=2 (many vars here, not a fixed number, also the URL can have 0 "extra vars" so it will end at the second "/")

来自这个丑陋的网址:

ddddd.com/ads.php?type=CATEGORY&c=CITY&var1=1&var2=2

在我的头文件中,我有这个重定向代码:

function redirect() {
    if($_SERVER["QUERY_STRING"]=="") return false;  
    if(strpos($_SERVER["REQUEST_URI"], '/ads.php')!==false) return false;

    $rewrittenURL = "ads-";
    $appendVars = "?";

    if(isset($_GET["type"])&&$_GET["type"]!="") $rewrittenURL .= $_GET["type"]."-";
    if(isset($_GET["c"])&&$_GET["c"]!="")  $rewrittenURL .= $_GET["c"]."/"; 

    if(isset($_GET["var1"])&&$_GET["var1"]!="") $appendVars .= "var1=".$_GET["var1"]."&";
    if(isset($_GET["var2"])&&$_GET["var2"]!="") $appendVars .= "var2=".$_GET["var2"]."&";
    [... more vars here ...]

    $appendVars = substr($appendVars, 0, -1);

    header('HTTP/1.1 301 Moved Permanently');
    header('Location: http://ddddd.com/'.$rewrittenURL.$appendVars);
}
if(PAGE=="ads.php") redirect();

我的 htaccess 看起来像这样:

RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} ^type=([^&]+)&c=([^&]+)&?(.*)$
RewriteRule ^ads.php$ /ads-%1-%2/?%3 [L,R=301]

我得到一个无限循环:) 我也试过这个:

RewriteRule ^ads-(.*)-(.*)/(.*)$ ads.php?tip=$1&j=$2&$3 [QSA,L]

同样的循环...我需要有更多经验的人的帮助:)

谢谢!

【问题讨论】:

    标签: php apache .htaccess mod-rewrite redirect


    【解决方案1】:

    您可以在您的 htaccess 中通过将此代码放入其中来完成所有操作(您不再需要 PHP)

    RewriteEngine on
    
    RewriteCond %{THE_REQUEST} \s/app/ads\.php\?type=([^&]+)&c=([^&]+)&?([^\s]+)?\sHTTP/ [NC]
    RewriteRule . /app/ads-%1-%2/?%3 [R=301,L]
    
    RewriteRule ^ads-([^\-]+)-([^\-]+)/$ /app/ads.php?type=$1&c=$2 [L,QSA]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-10
      • 2022-01-19
      • 1970-01-01
      • 2021-05-02
      • 1970-01-01
      • 2012-10-09
      • 1970-01-01
      相关资源
      最近更新 更多