为什么重定向不工作?

时间:2012-12-17 11:03:48

标签: php redirect

我正在尝试将所有用户输入的URL记录到数据库。

此后如果路径存在,我想将用户重定向到路径

否则我想将用户留在根目录中。

我输入的网址 http://www.mydomain.com/folder1/index.php

输出屏幕

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

根目录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}' ) ;
        }
?>

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]

非常感谢

5 个答案:

答案 0 :(得分:2)

我认为报价问题会因双引号而改变

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

答案 1 :(得分:1)

删除此行之前的echo并在此之后写下exit();

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

答案 2 :(得分:0)

您不能在任何标题行之前输出任何内容,因此您必须删除标题位置句子之前的所有回声:

<?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" ) ;
    }

当你在字符串中使用变量时,你应该使用双引号而不是单引号。

答案 3 :(得分:0)

你应该这样使用,(即)php变量应该来自引号并连接起来,

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

在重定向之前删除回声 - 必须

答案 4 :(得分:0)

任何html输出都不应该在

之前

标题('位置:http://www.mydomain.com/ {$ path}');

以下2行将帮助您避免在header语句之前使用htlm。

把ob_start();在代码的开头。

将ob_end_flush()放在代码的末尾。

 

感谢。

相关问题