使 htaccess 规则仅适用于当前文件夹 [英] Make htaccess rule apply only to the current folder

查看:19
本文介绍了使 htaccess 规则仅适用于当前文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I saw this example of how to change index.php to the url name

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

so what it does is change from www.site.com/map/index.php to www.site.com/map/country

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

it rewrite index.php to the country name in url, the problem is when I acess a directory above or sub directory like www.site.com/map/countryname/state it just replace theindex.phpinside the state directory to theindex.phpinside the map directory how to solve this ? or how to make this applied to the current directory only ?

这是网站目录结构http://ufile.io/3dii7 所以当我去site/map/state/ 它可以工作,但我需要 url 中的国家/地区名称来访问这样的状态目录 site/map/country/state/

Here is the site dir structure http://ufile.io/3dii7 so when I go to site/map/state/ it works but i need the country name in url to acess state dir like this site/map/country/state/

推荐答案

让你的 .htaccess 像这样:

Have your .htaccess as this:

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]

这篇关于使 htaccess 规则仅适用于当前文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆