apache - 使本地文件夹 .Htaccess 从 Root .Htaccess 导入全局规则
问题描述
我的网站中有一个特殊的文件夹,需要有自己的路由规则。所以我有自己的.htaccess
文件,代码如下:
DirectoryIndex router.php
Options -Indexes
Options +FollowSymLinks
RewriteEngine on
RewriteBase /categories/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/*([^/]*/?[^/]*)/*$ router.php?url=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/*$ catrouter.php?url=
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ 404.php [L,QSA]
但是使用此设置,我的根.htaccess
规则不起作用。
例如,从非 www 重定向到 www,从非 https 重定向到 https。
这是我的根结构:
Options -Indexes
RewriteEngine on
RewriteCond %{HTTP_HOST} !^subdom\. [NC]
###
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
###
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_USER_AGENT} ^(BADBOT1|BADBOT2) [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^$
RewriteRule ^.* - [F,L]
RewriteRule ^(.+?na\.jpg)$ /img/image.jpg [L,NC,QSA]
# Add Proper MIME-Type for Favicon
AddType image/x-icon .ico
# BEGIN EXPIRES
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/gif "access plus 1 month"
ExpiresByType text/css "access plus 1 week"
</IfModule>
# END EXPIRES
# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php72” package as the default “PHP” programming language.
<IfModule mime_module>
AddHandler application/x-httpd-php .php .php7 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit
我怎样才能使它特别.htaccess
遵循根默认设置的其他规则.htaccess
?
或者,我应该如何将规则从根复制到特殊的,以使其正常工作?
解决方案
/category
这是因为您的服务器正在从文件夹而不是根目录中读取 htaccess 规则。当您使用URI/category/.htaccess
从服务器请求某些内容时,会调用Your 。/category
您需要http to https
从您的 root/htaccess 复制规则并将其粘贴到其中/category/.htaccess
以使 https 重定向起作用。
DirectoryIndex router.php
Options -Indexes
Options +FollowSymLinks
RewriteEngine on
#force ssl plus www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
####
RewriteBase /categories/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/*([^/]*/?[^/]*)/*$ router.php?url=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/*$ catrouter.php?url=
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ 404.php [L,QSA]
推荐阅读
- identityserver4 - Identity Server 的 ProfileService 中的声明更改未反映在身份验证 Cookie 中
- iis - 将 .NET Core APP V2.2 托管到 IIS 错误 500
- microsoft-teams - 关于 MS Teams 申请流程的说明
- javascript - 无法在反应传单 next.js 中加载标记
- c# - Process.Start() 找不到文件?
- visual-studio-code - 使用“任何”字符进行 Visual Studio 代码搜索
- pandas - 在 Python 中对 DataFrame 循环逻辑回归
- javascript - XPath:查找
- 两个之间的兄弟元素
- 同为兄弟的元素
- azure - SignInSignup 登录后 Azure B2C 路由重置密码流只有几个用户
- arrays - 基于map的key过滤JSON数据