【问题标题】:htaccess redirect index.php to file, only if there are parametershtaccess 将 index.php 重定向到文件,只有在有参数的情况下
【发布时间】:2021-10-15 14:13:32
【问题描述】:

我正在尝试实现仅当 index.php 中有参数时才会发生的 htaccess 重定向

结果应该是:

  • 如果我调用 mydomain.com 或 mydomain.com/index.php,我应该会看到 index.php(通常)

  • 如果我调用 mydomain.com/myvar 它应该重定向到 script.php?var=$1

  • 但是如果我调用其他文件(例如 mydomain.com/page.php)或其他目录(例如 mydomain.com/folder/),我应该可以正常看到这些文件,而无需任何重定向。

这可能吗?这是我到目前为止所得到的:

RewriteRule ^(.*)$ script.php?var=$1 [L,QSA]

但它确实会重定向所有内容,包括 /index.php(不带参数)、/page.php 和 /folder/

【问题讨论】:

  • RewriteCond %{REQUEST_FILENAME} !-d 的用途。!-d 表示不是directroy

标签: php .htaccess redirect mod-rewrite


【解决方案1】:

您可以使用以下规则:

RewriteEngine on

#not a file
RewriteCond %{REQUEST_FILENAME} !-f
#not a directory
RewriteCond %{REQUEST_FILENAME} !-d
#rewrite the request path to script.php
RewriteRule ^(.+)$ script.php?var=$1 [L,QSA]

我还将您的正则表达式模式更改为(.+),以防止您的主页重定向/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-26
    • 2016-11-30
    • 1970-01-01
    相关资源
    最近更新 更多