Vishnu R Nair
Vishnu R Nair

Reputation: 317

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 to http://localhost/Vishnu/ReDirectionProject/TEST/somePhpPage.php and in the somePhpPage.php I have a header() to redirect to another 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=3and 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

So I try to use this .htaccess code

 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

EDIT :

PHP Page :

  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();

Upvotes: 2

Views: 1058

Answers (2)

iam-decoder
iam-decoder

Reputation: 2564

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

RewriteEngine On

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

Upvotes: 1

anubhava
anubhava

Reputation: 786091

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]

Upvotes: 1

Related Questions