【问题标题】:htaccess file path to arrayhtaccess 文件路径到数组
【发布时间】:2015-10-16 21:25:14
【问题描述】:

我对 htaccess 做的不多,通常使用已经制作好的框架。我检查了多个站点来学习 htaccess,但它们都遇到了一些难以掌握的情况。我尝试了一些乱码,并在stackoverflow上查看其他地方,我发现了一些可以执行第一个目录或有限集的内容,但没有进一步,而且我没有运气。

我正在寻找的是这样做......

用户在地址栏中输入(或浏览到)以下内容之一...

example.com
example.com/foo/bar/
example.com/some/really/long/set/of/directories/.../that/seems/to/go/on/forever

并最终将所有重定向到由处理

example.com/index.php

用一个数组填充路径如

// in index.php
print_r (myarray);
// printing something like...
// []
// or
// ["foo", "bar"]
// or
// ["some", "really", "long", "set", "of", "directories", ... , "that", "seems", "to", "go", "on", "forever"]
// respectively.

【问题讨论】:

    标签: php apache .htaccess directory


    【解决方案1】:

    您可能会捕获主机部分之后的所有内容并将其附加到.htaccess 中的dir 变量:

    RewriteRule ^example.com(.*) http://example.com?dir=$1 [QSA,L]
    

    或者:

    RewriteRule ^%{HTTP_HOST}(.*) http://%{HTTP_HOST}?dir=$1 [QSA,L]
    

    然后在您的 PHP 中,您可以根据需要使用 $_GET['dir']

    $array = explode('/', $_GET['dir']);
    

    【讨论】:

    • 嗯...不适合我,但可能是我的网站的问题。我得搞点它。
    • 发现问题的一部分是我需要RewriteEngine on,但是它给了我 404 而不是重定向到 index.php
    • 是否有某种重写条件需要与规则绑定才能使其工作?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-12
    • 2016-11-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多