【问题标题】:htaccess file in custom mvc application [closed]自定义mvc应用程序中的htaccess文件[关闭]
【发布时间】:2012-04-15 12:03:39
【问题描述】:

我正在使用 php 构建自己的 MVC 应用程序。 index.php 将是前端控制器。 在 index.php 中,我将使用 $_SERVER['QUERY_STRING'] 获取参数,所以我需要我的

.htaccess 文件将 /attr1/attr2/attr3 重定向到 index.php?attr1/attr2/attr3。

($_SERVER['PHP_SELF'] 不能正常工作)

如果我的 htaccess 文件(apache 服务器)是

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(.*)$ index.php?$1
</IfModule>

query_string 输出 index.php 但是如果我通过 localhost/something/attr1/attr2/attr3 访问 url 并且 .htaccess 是

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(something)\/(.*)$ index.php?$2
</IfModule>

query_string 正在打印 attr1/attr2/attr3

WTF??

【问题讨论】:

    标签: php .htaccess url-rewriting


    【解决方案1】:

    我喜欢这样:

    <IfModule mod_rewrite.c>
        RewriteEngine on
        RewriteBase /
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ index.php?url=$1 [NC]
    </IfModule>
    

    然后 url 将在 $_GET 数组中(第 4 行和第 5 行确保该规则在该路径中没有实际文件时适用)

    【讨论】:

    • 不错!!所以我不得不把这个 RewriteCond!我会更好地研究它们!非常感谢!!
    猜你喜欢
    • 2013-03-16
    • 1970-01-01
    • 2017-03-18
    • 1970-01-01
    • 2012-09-20
    • 1970-01-01
    • 2012-09-25
    • 2017-11-30
    • 2017-01-22
    相关资源
    最近更新 更多