Htaccess重写此网址的规则

时间:2021-01-28 11:04:09

I just started to learn htaccess and i'd like to rewrite my current urls from this:

我刚刚开始学习htaccess,我想重写我现在的网址:

http://www.url.com/?location=script

To:

http://www.url.com/script

So far i've managed to do this but now i want to have a directory with more controllers so i can have something like this:

到目前为止我已经设法做到这一点,但现在我想拥有一个包含更多控制器的目录,所以我可以这样:

http://www.url.com/script/method

Structure: Script directory --> method.php

结构:脚本目录 - > method.php

Currently my directory structure for includes its like this:

目前我的目录结构包括如下:

assets-->client(directory):

  • login.php
  • logout.php
  • register.php
  • something.php

And i'd like to access these using a url like:

我想使用以下网址访问这些:

    url.com/client/login
    url.com/client/logout
    url.com/client/register
    url.com/client/something

My .htaccess:

    <ifModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?location=$1 [L]
</ifModule>

PHP based inclusion code:

基于PHP的包含代码:

####################################################################
#                       PARSE THE CURRENT PAGE                     #
####################################################################

  $includeDir =".".DIRECTORY_SEPARATOR."assets/controllers".DIRECTORY_SEPARATOR;
  $includeDefault = $includeDir."home.php"; 
    if(isset($_GET['ajaxpage']) && !empty($_GET['ajaxpage'])){
        $_GET['ajaxpage'] = str_replace("\0", '', $_GET['ajaxpage']);
        $includeFile =   basename(realpath($includeDir.$_GET['ajaxpage'].".php"));
        $includePath = $includeDir.$includeFile;
        if(!empty($includeFile) && file_exists($includePath)) {
            include($includePath);
        }
        else{
            include($includeDefault);
        }
        exit();
    } 

  if(isset($_GET['location']) && !empty($_GET['location']))
            {
                $_GET['location'] = str_replace("\0", '', $_GET['location']);
                $includeFile = basename(realpath($includeDir.$_GET['location'].".php"));
                $includePath = $includeDir.$includeFile;

                if(!empty($includeFile) && file_exists($includePath)) 
                {
                    include($includePath);
                }
                else 
                {
                    include($includeDefault);
                }

            } 
            else 
            {
                include($includeDefault);
            }

All my controllers are in assets/controllers/ucp/login.php for example.

例如,我的所有控制器都在assets / controllers / ucp / login.php中。

1 个解决方案

#1


How about:

<ifModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} ^(/client/[a-zA-Z0-9_\-]+)$
    RewriteRule ^[a-z]+ index.php?location=$2 [L]
</ifModule>

It does include a leading slash and doesn't have the PHP extension. But this can be altered in PHP to suit your needs.

它确实包含一个前导斜杠,并且没有PHP扩展。但这可以在PHP中进行更改以满足您的需求。

Be wary though, your code gives access to all your PHP files. You might want to check $_GET['location'] against an array of allowed locations. Consider the following URL as example of how this could go wrong

但要小心,您的代码可以访问您的所有PHP文件。您可能希望针对允许的位置数组检查$ _GET ['location']。请考虑以下URL作为如何出错的示例

http://example.com/index.php?location=../drop_database.php

#1


How about:

<ifModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} ^(/client/[a-zA-Z0-9_\-]+)$
    RewriteRule ^[a-z]+ index.php?location=$2 [L]
</ifModule>

It does include a leading slash and doesn't have the PHP extension. But this can be altered in PHP to suit your needs.

它确实包含一个前导斜杠,并且没有PHP扩展。但这可以在PHP中进行更改以满足您的需求。

Be wary though, your code gives access to all your PHP files. You might want to check $_GET['location'] against an array of allowed locations. Consider the following URL as example of how this could go wrong

但要小心,您的代码可以访问您的所有PHP文件。您可能希望针对允许的位置数组检查$ _GET ['location']。请考虑以下URL作为如何出错的示例

http://example.com/index.php?location=../drop_database.php