我看到了如何将index.php更改为网址名称的示例
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
)时,它只是将state目录下的
index.php替换为
index.phpmap目录下的
如何解决?或者如何使其仅应用于当前目录?
这是站点目录结构 http://ufile.io/3dii7因此,当我转到 site/map/state/
时,它可以工作,但我需要 url 中的国家/地区名称才能访问州目录,如下所示 site/map/country/state/
最佳答案
让你的 .htaccess 如下:
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]
关于php - 使 htaccess 规则仅应用于当前文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43895224/