1美元替代Apache';重写规则不';t返回第一个匹配的表达式

1美元替代Apache';重写规则不';t返回第一个匹配的表达式,apache,.htaccess,mod-rewrite,url-rewriting,friendly-url,Apache,.htaccess,Mod Rewrite,Url Rewriting,Friendly Url,我到处都读到Apache重写引擎中的模式替换与正则表达式的预期效果一样,所以我尝试: .htaccess: RewriteRule(.*)index.php?路由=$1 但是如果请求get var路径中的domain.com/some/url,我会得到index.php,而不是some/url 在http varREDIRECT\u QUERY\u STRINGI getroute=some/url 但是在QUERY\u STRING中,我得到route=index.php 这里可能出了什么问题

我到处都读到Apache重写引擎中的模式替换与正则表达式的预期效果一样,所以我尝试:

.htaccess
RewriteRule(.*)index.php?路由=$1

但是如果请求get var路径中的
domain.com/some/url
,我会得到
index.php
,而不是
some/url

在http var
REDIRECT\u QUERY\u STRING
I get
route=some/url

但是在
QUERY\u STRING
中,我得到
route=index.php

这里可能出了什么问题

PS:
$0
还返回
index.php


如果我使用
重写规则。index.php?route=$1
,我得到route=
I
,无论请求URL是什么。

$1
按预期工作,但问题是您使用此模式:

(.*)
什么都配。您的重写规则实际上会循环并运行两次,因为您没有任何
RewriteCond
来进行循环

  • 首先,它为URI=
    some/url
    运行,URI变成
    index.php
    ,而
    $1
    变成
    some/url
  • 第二次运行URI=
    index.php
    时,
    $1
    变成
    index.php
  • 您可以使用此规则修复此行为:

    # If the request is not for a valid directory
    RewriteCond %{REQUEST_FILENAME} !-d
    # If the request is not for a valid file
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule (.*) index.php?route=$1 [L,QSA]
    
    或者,如果要将现有文件和目录重写为
    index.php
    ,请使用:

    RewriteRule ^((?!index\.php$).*)$ index.php?route=$1 [L,QSA,NC]
    

    这将把除
    index.php
    之外的所有内容重写为
    index.php

    谢谢!我得到了[L,QSA]的用法,但是重写条件呢?它们是针对这个特定问题的吗?因为我确实希望,即使请求与现有文件或目录名匹配,它们仍然会被定向到我更新的答案中的index.phpok检查规则,以重写所有文件/目录。谢谢,但只需添加标志就可以工作,即使不排除带有条件或正则表达式的
    index.php