使本地文件夹.Htaccess从根.Htaccess导入全局规则

时间:2019-04-11 15:47:03

标签: apache .htaccess mod-rewrite

我的网站上有一个特殊的文件夹,需要使用自己的路由规则。 因此,我有自己的.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默认设置的其他规则?

或者,我应该如何将规则从根目录复制到特殊规则中,以使其正常运行?

1 个答案:

答案 0 :(得分:0)

这是因为您的服务器正在从/category文件夹而不是根目录读取htaccess规则。当您使用/category/.htaccess URI向服务器请求某些内容时,将调用/category。您需要从root / 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]