删除子域名为.htaccess的.php扩展名?

时间:2013-02-20 11:13:28

标签: php .htaccess url-rewriting

我不是url-rewrites的专家。

我的域名为helloboy

我的子域名是wwwlabs

我想仅删除子域 labs 目录graph .php扩展名

来自: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]

2 个答案:

答案 0 :(得分:1)

也许这会有所帮助:

RewriteCond %{HTTP_HOST}  labs.helloboy.com$ 
RewriteRule ^graph/(.*)/?$ graph/$1.php [L,NC]

答案 1 :(得分: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)

如果您需要进一步简化以了解这一具有启发性的解决方案,请与我们联系。