【问题标题】:Mod Rewrite load index.php on bad pathMod Rewrite 在错误路径上加载 index.php
【发布时间】:2013-12-07 17:30:20
【问题描述】:

这些是我正在使用的重写规则。在这一点上,我试图强制错误的 url 加载 index.php。 html标签<base href="/directory-name/" />在index.php的顶部,所以我的css、图片和其他资源加载正常。

Options -Indexes +FollowSymLinks
RewriteEngine On 

RewriteCond ${REQUEST_URI} ^.+$
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml)$ [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]

RewriteRule ^(.*)/(.*)/(.*) /index.php?x=$1&y=$2&z=$3 [L]
RewriteRule ^(.*)/(.*) /index.php?x=$1&y=$2 [L]
RewriteRule ^(.*) /index.php?x=$1 [L]

网址示例:

www.domain.com/directory-name/
www.domain.com/directory-name/index.php
www.domain.com/directory-name/index.php/arg1/arg2/arg3

www.domain.com/directory-name/anything-here
www.domain.com/directory-name/anything-here/arg1/arg2/arg3

前 3 个 url 示例工作正常并加载 index.php。但是第 4 和第 5 个 url 失败并将我转储到 html 根目录 / 存在 xampp 索引的位置。我假设最后一次 url 重写失败是因为 index.php 位于根目录下的子目录中?

我将如何解决这个问题?即使路径中有其他内容,我如何强制 URL 加载 www.domain.com/directory-name/index.php

【问题讨论】:

    标签: php regex apache .htaccess mod-rewrite


    【解决方案1】:

    试试这些规则:

    RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?x=$1&y=$2&z=$3 [L]
    RewriteRule ^([^/]+)/([^/]+)/?$ index.php?x=$1&y=$2 [L]
    RewriteRule ^([^/]+)/?$ index.php?x=$1 [L]
    

    【讨论】:

    • 对于奖金回合,如果文件是 extras-404.php,您将如何添加自定义错误 404?
    • ErrorDocument 404 /extras-404.php 放在顶部。
    • 提供其中一些 URL 的示例?
    • 原来是我的一个错误。对不起。我应该删除那条评论。
    猜你喜欢
    • 2015-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多