rotem
rotem

Reputation: 15

Why redirect not working?

I am trying to log to data base all URL that user type.

After this if the path exists I want to redirect the user to path

Else I want to leave the user in the root directory.

The URL that I type http://www.mydomain.com/folder1/index.php

The output screen

folder1/index.php
Array ( [0] => folder1 [1] => index.php ) url = to folder 1
folder1/index.php

The root directory index.php

<?php
        if ($_GET['url']!= "") {
            // url not empty there is query string
            $url = $_GET['url'];
            $url = rtrim($url, '/');

            echo ($url.'<br />');
            $url = explode('/', $url);
            $file = $url[0];
            print_r($url);
        }

        else // url empty it is root directory
            {echo ('<br /> url empty <br />');}

        if ($url[0] == 'folder1'){
            echo ('url = to folder 1<br />');
            $path = $url[0].'/index.php';

            //the path exists do redirect to folder1/index.php
            echo $path ;
                 header( 'Location: http://www.mydomain.com/{$path}' ) ;
        }
?>

The Htaccess

RewriteEngine on
Rewritecond %{HTTP_HOST} !^www\.mydomain\.com
RewriteRule (.*) //www.mydomain.com/$1 [R=301,L]
RewriteRule \.(sql)$ - [F]
RewriteRule \.(txt)$ - [F]
RewriteRule \.(zip)$ - [F]
RewriteRule \.(rar)$ - [F]

<IfModule mod_rewrite.c>
# For security reasons, Option followsymlinks cannot be overridden.
#  Options +FollowSymLinks
   Options +SymLinksIfOwnerMatch
   Options -Indexes
   RewriteEngine On
   RewriteCond %{http_host} ^mydomain.com [NC]
   RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,NC]
   RewriteRule ^([^\.]+)$ index.php?url=$1 [QSA,L,NE]
</IfModule>


RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L,NE]

Many thanks

Upvotes: 1

Views: 119

Answers (5)

Nadun
Nadun

Reputation: 1

Any html out put should not be there before the

header( 'Location: http://www.mydomain.com/{$path}' ) ;

Following 2 lines will be helped you to avoid htlm before the header statement.

Put ob_start(); in begin of your code.

Put ob_end_flush() end of your code.

Thanks.

Upvotes: 0

Vinoth Babu
Vinoth Babu

Reputation: 6852

You should use like this, (i.e) php variable should come out of quotes and get concatenated,

header( "Location: http://www.mydomain.com/".$path) ;

Remove the echo before redirecting - MUST

Upvotes: 0

Jester
Jester

Reputation: 1303

You cannot have any output before any header line, so you have to remove all echoes before the header location sentence:

<?php
    if ($_GET['url']!= "") {
        // url not empty there is query string
        $url = $_GET['url'];
        $url = rtrim($url, '/');

        $url = explode('/', $url);
        $file = $url[0];
    }

    else // url empty it is root directory
        {}

    if ($url[0] == 'folder1'){
        $path = $url[0].'/index.php';

        //the path exists do redirect to folder1/index.php
        header( "Location: http://www.mydomain.com/$path" ) ;
    }

And as you are using a variable inside the string, you shall use double quotes instead of single ones.

Upvotes: 0

senK
senK

Reputation: 2802

I think its the quote problem change with double quotes

             header( "Location: http://www.mydomain.com/{$path}" ) ;

Upvotes: 2

Yogesh Suthar
Yogesh Suthar

Reputation: 30488

Remove the echo before this line and write exit(); after that

header( "Location: http://www.mydomain.com/{$path}" );
exit();

Upvotes: 1

Related Questions