简体   繁体   English

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

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

I have a URL like , http://localhost/Vishnu/ReDirectionProject/TEST/SubDomain/aa/bb/abc.php?e=1&b=3 when I try to run this URL ie, if it contain any string like "SubDomain" it should redirect me tohttp://localhost/Vishnu/ReDirectionProject/TEST/somePhpPage.php and in the somePhpPage.php I have a header() to redirect to another URL.当我尝试运行此 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我有一个header()重定向到另一个 URL。 So basic problem is when I try to run the URL with string "SubDomain" I need to parse the string after "SubDomain" here it's aa/bb/abc.php?e=1&b=3 and get this values into somePhpPage.php and there I convert/add some predefined URL to it for example : http://localhost/Vishnu/ReDirectionProject/TEST/somePhpPage.php/aa/bb/abc.php?e=1&b=3所以基本的问题是,当我尝试使用字符串“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

So I try to use this .所以我尝试使用 this 。 htaccess code htaccess代码

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

it works fine except when the URL does not have the query like abc.php if it contain any query like abc.php?e=1&b=3 it shows ERR_TOO_MANY_REDIRECTS error它工作正常,除非 URL 没有像abc.php这样的查询,如果它包含像abc.php?e=1&b=3这样的查询,它会显示 ERR_TOO_MANY_REDIRECTS 错误

EDIT :编辑 :

PHP Page : 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();

You can use this rule:您可以使用此规则:

RewriteEngine On

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

This should work, the QSA flag will attach the original querystring to the rewritten url and you can specific the path that should redirect inside the RewriteCond line这应该有效,QSA 标志会将原始查询字符串附加到重写的 url,您可以指定应该在RewriteCond行内重定向的路径

RewriteEngine On

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM