制作本地文件夹 .Htaccess 从 Root 导入全局规则.呵呵



我的网站中有一个特殊的文件夹,需要有自己的路由规则。 所以我有自己的.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规则。 当您使用/categoryURI 从服务器请求某些内容时,将调用您的/category/.htaccess。 您需要从根/htaccess 复制http to https规则并将其粘贴到/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]

最新更新