2

私はURLリライトの専門家ではありません。

私のドメインはhelloboyです。

私のサブドメインはとwwwですlabs

サブドメインディレクトリの.php拡張子のみを削除したいですか? labs graph

差出人:http ://labs.helloboy.com/graph/load/show.php

宛先:http ://labs.helloboy.com/graph/load/show/

どうやってやるの?

これが私のコードです.htaccess

 RewriteEngine on
 RewriteBase /
 RewriteCond %{HTTP_HOST} ^labs\.helloboy\.com$ /$1.php -f
 RewriteRule ^(graph/load/.*?)/?$ $1.php [L,NC]
4

2 に答える 2

1

多分これは役立つでしょう:

RewriteCond %{HTTP_HOST}  labs.helloboy.com$ 
RewriteRule ^graph/(.*)/?$ graph/$1.php [L,NC]
于 2013-02-20T11:48:07.630 に答える
1

たとえばgraph/index.php、グラフの書き換え要求を処理するファイルをグラフの下に作成することをお勧めします (または文字通りどこでも、それはあなた次第です)。次に、show.php(およびリダイレクトする他のファイル) を、処理に適した別のフォルダー (たとえば、graph/lib.

ここで、 http://labs.helloboy.com/graph/に続くすべてが「存在しない」と想像してください。パスに基づいてリクエストを処理するだけです ( http://labs.helloboy.com/graph/load/showなど)。 )。

架空のファイル構造は次のようなものです。

root (in your case it would prolly be www/labs)
-- .htaccess
-- graph
   -- index.php (handles rewrites, can be anywhere you want but it must be reflected in the .htacces file)
   -- lib (location to store the existing files, can be anywhere you want)
      -- show.php
      -- delete.php
      -- ..etc

まず、リクエストを処理し、.php リダイレクトも行うルールを .htaccess に追加しましょう。

.htaccess

RewriteEngine on

# Make sure index.php requests are NOT rewritten, otherwise we'll end up in an endless loop.
RewriteCond %{HTTP_HOST} ^labs\.helloboy\.com$
RewriteCond %{REQUEST_URI} ^graph/index\.php$
RewriteRule .* - [L,NC,QSA]

# Redirect all .php files and remove the extension
RewriteCond %{HTTP_HOST} ^labs\.helloboy\.com$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(graph/.+)\.php$ $1 [R,NC,QSA]

# Make sure that we won't match existing files or directories, otherwise the lib files will fail. Get the path after "graph/" and add it as a path argument to "graph/index.php".
RewriteCond %{HTTP_HOST} ^labs\.helloboy\.com$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^graph/(.*) graph/index.php?path=$1 [L,NC,QSA]

次に、ハンドラーに送信されたパス引数を処理して、何か役に立つことを行う必要があります。DEBUG コメントに注意してください。その下の呼び出しにより、後で表示する出力が作成されます。

グラフ/index.php

<?php

// NOTE: Consider returning a 404 status code with the header and possibly a redirect if you can't process the paths sanely.

//======
// DEBUG
//======

?><pre><?php var_export( $_GET ) ?></pre><?php

$path = isset( $_GET['path'] ) ? $_GET['path'] : '';

if ( $path ) {
    // Split the path into parts. For example load/show/ becomes an array with two elements: ['load', 'show']
    $paths = array_filter( explode( '/', $path ) );

    //======
    // DEBUG
    //======


    ?><pre><?php var_export( $paths ) ?></pre><?php

    $len = count( $paths );

    if ( $paths ) {
            // If we have any path we attempt to process it

        if ( $paths[0] === 'load' ) {

            // Do something if load

            if ( $len > 1 ) {
                $action = $paths[1];

                switch ( $action ) {
                    case 'show':
                        $file = dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . "$action.php";

                        //======
                        // DEBUG
                        //======

                        var_dump( $file ) ;

                        if ( file_exists( $file ) ) 
                            require_once $file;
                    break;
                }
            }
        }
    }
}

?>

パスhttp://labs.helloboy.com/graph/load/show/の場合、スクリプトは次のように出力します。

// This is the contents of the supervariabel $_GET
array (
  'path' => 'load/show/',
)

// This is the path parts after splitting and filtering of the path argument
array (
  0 => 'load',
  1 => 'show',
)

// Here we illustrate that we can include/process any file we wish based on the path contents
string '/your/server/path/graph/lib/show.php' (length=xx)

この示唆的な解決策を理解するためにさらに単純化する必要がある場合はお知らせください。

于 2013-02-20T13:43:20.913 に答える