1

ユーザーが入力したすべての URL をデータベースに記録しようとしています。

この後、パスが存在する場合、ユーザーをパスにリダイレクトしたい

それ以外の場合は、ユーザーをルート ディレクトリに残しておきます。

入力した 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]

どうもありがとう

4

5 に答える 5

2

二重引用符で引用符の問題が変わると思います

             header( "Location: http://www.mydomain.com/{$path}" ) ;
于 2012-12-17T11:07:19.190 に答える
1

echoこの行の前を削除し、exit();その後に記述します

header( "Location: http://www.mydomain.com/{$path}" );
exit();
于 2012-12-17T11:06:21.967 に答える
0

このように使用する必要があります。(つまり) php 変数は引用符から出て連結されます。

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

リダイレクトする前にエコーを削除する - MUST

于 2012-12-17T11:20:30.977 に答える
0

html の出力は、

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

次の 2 行は、ヘッダー ステートメントの前に htlm を避けるのに役立ちます。

ob_start(); を入れます。コードの先頭に。

コードの末尾に ob_end_flush() を配置します。

ありがとう。

于 2012-12-17T11:25:38.730 に答える
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" ) ;
    }

また、文字列内で変数を使用しているため、一重引用符ではなく二重引用符を使用する必要があります。

于 2012-12-17T11:07:37.700 に答える