【问题标题】:redirect url if it contain a specific words using .htaccess and php如果它包含使用 .htaccess 和 php 的特定单词,则重定向 url
【发布时间】:2016-03-14 06:06:59
【问题描述】:

当我尝试运行此 URL 时,我有一个类似 http://localhost/Vishnu/ReDirectionProject/TEST/SubDomain/aa/bb/abc.php?e=1&b=3 的 URL,即,如果它包含任何类似“SubDomain”的字符串,它应该将我重定向到 http://localhost/Vishnu/ReDirectionProject/TEST/somePhpPage.phpsomePhpPage.php 我有一个header() 重定向到另一个URL。 所以基本问题是当我尝试使用字符串“SubDomain”运行 URL 时,我需要解析“SubDomain”之后的字符串,这里是 aa/bb/abc.php?e=1&b=3 并将这些值转换为 somePhpPage.php,然后我转换/添加一些预定义的 URL例如:http://localhost/Vishnu/ReDirectionProject/TEST/somePhpPage.php/aa/bb/abc.php?e=1&b=3

所以我尝试使用这个 .htaccess 代码

 RewriteEngine On
 RewriteCond %{REQUEST_URI} SubDomain
 RewriteRule .* http://localhost/Vishnu/ReDirectionProject/TEST/UrlRedirect.php [L,R=301]

它工作正常,除非 URL 没有像 abc.php 这样的查询 如果它包含像abc.php?e=1&b=3 这样的任何查询,它会显示 ERR_TOO_MANY_REDIRECTS 错误

编辑:

PHP 页面:

  function urlRedirection() {
    $link       = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    $pageName       = basename($_SERVER['SCRIPT_NAME']);
    $rmvPath        = trim(strip_tags(substr($link,strpos($link,$pageName))));  
    if (strrpos($rmvPath,"?")) {
        $rmvPath        = substr($rmvPath,0,strrpos($rmvPath,"?")); 
    }
    $cnvFolders     = explode("/",$rmvPath);
    $folderIndex    = 0 ;
    $allwdExtns     = array(".php",".html",".asp",".jsp");
    for ($i=0;$i<count($cnvFolders);$i++) {
        if (strpos($cnvFolders[$i],".")) {
            $allwdExtChk    = substr($cnvFolders[$i],strpos($cnvFolders[$i],"."));
            if (in_array($allwdExtChk,$allwdExtns)) {
                $phpFiles[] = $cnvFolders[$i];  
            }
        }
        if (strpos($cnvFolders[$i],".")) {
            $cnvFolders[$i]     = "";   
        }
    }
    $lastFile   = count($phpFiles)-1; 
    $crtFolder  = implode("/",$cnvFolders) . "<br>";
    $crtFolder  = trim(strip_tags($crtFolder),"/");
    if (!file_exists($crtFolder)) {
        mkdir( $crtFolder, 0777, true );
    }
    $crtFile    = fopen("$crtFolder/$phpFiles[$lastFile]", "w") or die("Unable to open file!");

    $parameters = substr($link,strpos($link,"?")+strlen("?"));
    $cnvParamts = explode("&",$parameters);
    foreach ($cnvParamts as $paramWriting) {
        $paramWriting   = '$'.$paramWriting .';';
        $writeFile      = fwrite($crtFile,$paramWriting);   
    }

    $subdomainSelect    = substr($link,strpos($link,"date.")+strlen("date."));
    $curDate    = date('Y-m-d');
    $date = new DateTime($curDate);
    $date->modify('+1 day');
    $tommorrow  = $date->format('Y-m-d');
    $date->modify('+1 day');
    $dayAfterTommorrow  = $date->format('Y-m-d');
    $chkInTime          = substr($subdomainSelect,strpos($subdomainSelect,"checkin")+strlen("checkin"));
    if (strpos($chkInTime,"&")) {
        $chkInTime          = substr($chkInTime,0,strpos($chkInTime,"&"));
    }
    $chkInTime          = trim(strip_tags($chkInTime),"=");
    $chkOutTime = substr($link,strpos($link,"checkout")+strlen("checkout"));
    if (strpos($chkOutTime,"&")) {
        $chkOutTime = substr($chkOutTime,0,strpos($chkOutTime,"&"));
    }
    $chkOutTime = trim(strip_tags($chkOutTime),"=");
    if (strpos($subdomainSelect,"checkin")) {
        $subdomainSelect    = str_replace($chkInTime,$tommorrow,$subdomainSelect);      
    } else {
        $subdomainSelect    = $subdomainSelect."&checkin=$tommorrow";   
    }
    if (strpos($subdomainSelect,"checkout")) {
        $subdomainSelect    = str_replace($chkOutTime,$dayAfterTommorrow,$subdomainSelect);
    } else {
        $subdomainSelect    = $subdomainSelect."&checkout=$dayAfterTommorrow";
    }
    $subdomainSelect    = trim($subdomainSelect,"//");
    $subdomainSelect    = "http://".$subdomainSelect;
    if ($subdomainSelect) { 
        header("location:$subdomainSelect");
    }
}
urlRedirection();

【问题讨论】:

标签: php .htaccess redirect


【解决方案1】:

你可以使用这条规则:

RewriteEngine On

RewriteCond %{THE_REQUEST} /TEST/SubDomain/(\S+)\sHTTP [NC]
RewriteRule ^ /Vishnu/ReDirectionProject/TEST/UrlRedirect.php/%1 [L,R=301,NE]

【讨论】:

  • 谢谢,但它显示 ERR_TOO_MANY_REDIRECTS 错误
  • 我已经清除了历史记录,除了您在我的 .htaccess 文件中给定的代码之外,没有任何其他内容仍然是相同的错误
  • 它也适用于我的,请给我们您的整个 htaccess 文件。我敢打赌是其他原因导致您的问题
  • @anubhava ,错误来自我的自定义 php 文件,终于解决了,非常感谢
  • 1 为您宝贵的时间
【解决方案2】:

这应该可行,QSA 标志会将原始查询字符串附加到重写的 url,您可以在 RewriteCond 行内指定应该重定向的路径

RewriteEngine On

RewriteCond %{REQUEST_URI} ^(.*)SubDomain\/aa\/bb\/abc.php(.*)$ [NC]
RewriteRule ^ /Vishnu/ReDirectionProject/TEST/somePhpPage.php [L,R=301,QSA]

【讨论】:

  • @VishnuRNair 你能发布更多你的 htaccess 文件吗?你可能有其他事情导致重定向循环
  • 此代码不会将我重定向到我需要的页面,但它会显示文件未找到错误。
  • @VishnuRNair 如果您将目录“TEST”重命名为“TEST1”之类的不同名称,是否可以解决您的问题?如果它在正则表达式修饰符之前,我相信 apache 会在重写期间删除它
  • @VishnuRNair 我更新了我的答案,看看是否适合你
  • 您提供给我们的信息越多,我们就能越好地帮助您
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-23
  • 1970-01-01
  • 2014-02-26
  • 2014-03-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多