如果它包含使用.htaccess和php的特定单词,则重定向url

时间:2016-03-14 06:06:59

标签: php .htaccess redirect

当我尝试运行此网址时,我有一个类似http://localhost/Vishnu/ReDirectionProject/TEST/SubDomain/aa/bb/abc.php?e=1&b=3的网址,即,如果它包含任何字符串,例如“SubDomain”,则应将我重定向到http://localhost/Vishnu/ReDirectionProject/TEST/somePhpPage.phpsomePhpPage.php我有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]

它工作正常,除非网址没有像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();

2 个答案:

答案 0 :(得分:1)

您可以使用此规则:

3

答案 1 :(得分:1)

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

RewriteEngine On

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