2

我不是 url-rewrites 方面的专家。

我的域名是helloboy.

我的子域是wwwlabs

我想仅删除子域目录的.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

我建议您在图形下创建一个文件(或者实际上任何地方,这取决于您),例如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

首先,让我们在 .htaccess 中添加将处理请求并执行 .php 重定向的规则:

.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 回答
1

也许这会有所帮助:

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