Htaccess重写此网址的规则

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

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

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:

####################################################################
#                       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.

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.

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

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