【问题标题】:Htaccess rewrite rules for this url此 url 的 Htaccess 重写规则
【发布时间】:2015-05-11 12:06:38
【问题描述】:

我刚开始学习 htaccess,我想从这里重写我当前的网址:

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

收件人:

http://www.url.com/script

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

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

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

目前我的目录结构是这样的:

资产-->客户(目录):

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

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

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

我的 .htaccess:

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

基于 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);
            }

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

【问题讨论】:

  • 不回答你的问题,考虑在PSR-2 guide之后编写你的PHP。其他 PHP 程序员可能更愿意帮助您。
  • 以后我会考虑你的建议。

标签: php .htaccess url rewrite


【解决方案1】:

怎么样:

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

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

但请注意,您的代码可以访问您的所有 PHP 文件。您可能希望根据允许的位置数组检查 $_GET['location']。考虑以下 URL 作为这可能出错的示例

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

【讨论】:

  • 你从哪里得到$2
  • 每个包含的脚本都在 assets/controllers 目录中,然后我有另一个包含脚本的目录。例如:assets/controllers/error/traceback.php。使用您的代码,网址:http://.../error/traceback 它不起作用!。我还用 /assets/controllers/error/ 更改了 /client/
  • @MikeRockett $2 是正则表达式的第二个匹配项。 “客户”是第一个。
  • @AndreiVlad 如果您的代码是 3 层而不是 1 层,请将 $2 替换为 $4。试试htaccess.madewithlove.be。它将我的 htaccess 解析为有效且有效。
  • 您能否提供更多信息,例如:您遇到了什么错误?我之前评论提供的 URL 告诉你什么?
猜你喜欢
  • 2011-12-06
  • 2016-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多