【问题标题】:htaccess rewrite to force index.phphtaccess 重写以强制 index.php
【发布时间】:2012-01-06 01:09:45
【问题描述】:

我有这种格式的链接:http://www.EXAMPLE.com/index.php?page=pageid 它们也可以在:http://www.EXAMPLE.com/?page=pageid

如何重定向那些没有 index.php 文件名的文件以使用 url 的 index.php 部分?

我的 htaccess 文件中有其他重定向规则,它们工作正常,我只是不知道如何强制 index.php 部分并包含查询字符串。

【问题讨论】:

    标签: .htaccess url mod-rewrite


    【解决方案1】:

    将以下规则放入站点根目录下的 .htaccess 文件中

    RewriteEngine On
    RewriteBase /
    
    #only for the root directory
    RewriteCond %{REQUEST_URI} ^/$
    #if the uri is not already index.php
    RewriteCond %{REQUEST_URI} !^/index.php [NC]
    RewriteRule ^$ /index.php    [R=301,L]
    

    如果与您的其他规则仍然存在冲突,您可以通过在上面的重写规则之前添加以下条件,进一步将此规则限制为仅具有 page= 查询字符串参数的请求

    # to limit further to only the requests with a page= param
    RewriteCond %{QUERY_STRING} ^([^&]+&)*page=.*$ [NC]
    

    【讨论】:

    • 这不起作用。它不会重定向我最初发布的链接,并且会破坏一些不应该的链接。
    • 我通过添加另一个条件来限制此规则的范围。让我知道这是否有效。
    猜你喜欢
    • 2013-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-15
    相关资源
    最近更新 更多