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

标签 php apache .htaccess mod-rewrite

我看到了如何将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/

相关文章:

php - 使用 mediawiki api 获取特定页面的完整历史记录

angularjs - 如何在 apache htaccess 中为 angularjs 应用程序重写 url

php - 重定向到请求子文件夹的 index.php

php - 将 url 路径中的语言更改为子域的正则表达式

Javascript:如何打印 $_SESSION 数组中的值?

apache - mod_rewrite 快速且低资源的方式来阻止请求

apache - htaccess/mod_expires - 缓存特定文件

php - 自定义 404 错误页面——仅适用于 PHP 页面

php - Mysql子查询带百分比分割(难查询)

apache - .htaccess - 删除扩展