Php 使htaccess规则仅应用于当前文件夹
我看到了如何将index.php更改为url名称的示例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]+)/?$
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个条件仅用于第一个重写规则
。您可以通过在单独的排除规则中使用它们来避免条件的重复。它仍在将dirwww.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。我已经根据您的评论更新了答案,但请不要通过评论部分添加新的要求。您可以为这些情况创建一个新问题。