【问题标题】:Apache rewrite empty regex (root)Apache重写空正则表达式(根)
【发布时间】:2014-12-15 11:56:43
【问题描述】:

我在 Windows 8 笔记本电脑(WAMP 服务器)上运行了 Apache 2.4.4,我发现 .htaccess mod_rewrite 规则的行为非常奇怪。

我想将我网站的根目录重定向到特定文件。我的 .htaccess 看起来像这样:

RewriteEngine On
RewriteBase /
RewriteRule $^ /static/home.php [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ router.php?url=$1 [L,QSA]

仅当根目录中没有 index.php 文件时才有效。当我创建 index.php 文件时,Apache 将空路径直接重定向到索引文件,并且不会打扰第一个 RewriteRule。

有没有办法让这些 RewriteRules 和 index.php 文件一起工作?换句话说,我的示例有效,但我想将 router.php 重命名为 index.php 并保持两个 RewriteRules 正常工作。

谢谢!

【问题讨论】:

    标签: apache .htaccess mod-rewrite redirect document-root


    【解决方案1】:

    您可以调整您的正则表达式模式以在这两种情况下工作:

    DirectoryIndex something-else.php
    RewriteEngine On
    
    RewriteRule ^$ /static/home.php [L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
    

    【讨论】:

    • 是的,可以做到。不幸的是,我不能使用它,因为我可能过于简化了我的示例。我需要 index.php 文件(我网站的路由器)处理的其他 URL。我要更新我的问题。你知道 Apache 为什么要这样做吗?
    • 我将等待您的编辑为您提供一些可能的解决方法
    • 好吧,这不起作用,但如果我将DirectoryIndex index.php 更改为DirectoryIndex something-else.php,它会起作用。谢谢,我不知道 DirectoryIndex!
    • 很高兴知道它成功了,您可以通过单击我的答案左上角的勾号将答案标记为已接受。
    • 是的,如果您按照我写的方式修复示例,那么我会将您的答案标记为解决方案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-13
    • 1970-01-01
    • 1970-01-01
    • 2016-02-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多