Php 使htaccess规则仅应用于当前文件夹

Php 使htaccess规则仅应用于当前文件夹,php,apache,.htaccess,mod-rewrite,Php,Apache,.htaccess,Mod Rewrite,我看到了如何将index.php更改为url名称的示例 RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-s RewriteRule ^([a-z0-9-_.]+)/?$ index.php?id=$1 [NC,L] RewriteRule ^([a-z0-9-_.]+)/([a-z0-9]+)/?$

我看到了如何将index.php更改为url名称的示例

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^([a-z0-9-_.]+)/?$ index.php?id=$1 [NC,L]
RewriteRule ^([a-z0-9-_.]+)/([a-z0-9]+)/?$ index.php?id=$1&goto=$2 [NC,L]
.htaccess
文件位于
www.site.com/map/
目录中

因此,它所做的是从
www.site.com/map/index.php
更改为
www.site.com/map/country

它将index.php重写为url中的国家名称,问题是当我访问上面的目录或子目录时,如
www.site.com/map/countryname/state
它只是
将国家目录中的
index.php
替换为地图目录中的
index.php
如何解决这个问题?或者如何使其仅应用于当前目录


这是站点目录结构,因此当我转到
site/map/state/
时,它可以工作,但我需要url中的国家名称来访问state目录,如
site/map/country/state/

所示:

RewriteEngine On

# skip all rules below this for files and directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

RewriteRule ^([\w-.]+)/?$ index.php?id=$1 [L,QSA]

RewriteCond %{DOCUMENT_ROOT}/site/map/$2 -f
RewriteRule ^([\w-]+)/(.+)/?$ $2?id=$1&goto=$2 [L,QSA]

RewriteCond %{DOCUMENT_ROOT}/site/map/$2/index.php -f
RewriteRule ^([\w-.]+)/([a-z0-9]+)/?$ $2/index.php?id=$1&goto=$2 [NC,L,QSA]

RewriteRule ^([\w-.]+)/([a-z0-9]+)/?$ index.php?id=$1&goto=$2 [NC,L,QSA]

您对
www.site.com/map/countryname/state
的预期输出是什么?我指的是你想要为这个
URL
执行什么,同时也要提到你收到的
500
错误消息,
404
403
可能
DirectoryIndex
具有根相对值
/index.php
,因此第二条规则将
countryname/state
/map/.htaccess
匹配。对于第二条规则,还应重复所有
RewriteCond
设置,为了不影响文件系统路径。
RewriteCond
仅对下一个
RewriteRule
有效。因此,您的3个条件仅用于第一个
重写规则
。您可以通过在单独的排除规则中使用它们来避免条件的重复。它仍在将dir
www.site.com/map/country/state
中的
index.php文件替换为
www.site.com/map/
中的index.php文件,以使其应用于当前的/map/dir。这就是.htacess文件
map
是目录,所以countryname是地图目录中的index.php文件,当前的.htaccess规则将把index.php重写为国家名称。在
map
中有dir
/state/
这是下面的site dir结构,因此当我进入
site/map/state/
时,它可以工作,但我需要url中的国家名称,以便在输入
site/map/country/state/
时访问state dirhttp://example.com/map/abcd/state
它加载index.php内部
map
dir。我需要它来加载
state
dirI中的index.php。我已经根据您的评论更新了答案,但请不要通过评论部分添加新的要求。您可以为这些情况创建一个新问题。