重定向 URL,如果它包含使用 .htaccess 和 PHP 的特定单词


redirect url if it contain a specific words using .htaccess and php

我有一个类似 的 URL,http://localhost/Vishnu/ReDirectionProject/TEST/SubDomain/aa/bb/abc.php?e=1&b=3 当我尝试运行此 URL 时,即,如果它包含任何像"SubDomain"这样的字符串,它应该将我重定向到 http://localhost/Vishnu/ReDirectionProject/TEST/somePhpPage.php在somePhpPage.php我有一个重定向到另一个 URL 的header()。所以基本问题是当我尝试使用字符串"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=3ERR_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();

您可以使用此规则:

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

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

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