首页 > 解决方案 > 使本地文件夹 .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

或者,我应该如何将规则从根复制到特殊的,以使其正常工作?

标签: apache.htaccessmod-rewrite

解决方案


/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]

推荐阅读