.htaccess htaccess重写规则中出现错误

.htaccess htaccess重写规则中出现错误,.htaccess,mod-rewrite,.htaccess,Mod Rewrite,我试图确保,如果在安全子域之外使用HTTPS,它会被重定向到HTTP 这是我在root.htaccess文件中的内容: # Redirect HTTPS requests for non-SSL pages back to HTTP. (Note that shared objects # such as images are excluded from this rule) RewriteCond %{HTTPS} =on # my.EXAMPLE.com is the secure su

我试图确保,如果在安全子域之外使用HTTPS,它会被重定向到HTTP

这是我在root.htaccess文件中的内容:

# Redirect HTTPS requests for non-SSL pages back to HTTP. (Note that shared objects
# such as images are excluded from this rule)

RewriteCond %{HTTPS} =on

# my.EXAMPLE.com is the secure subdirectory.
RewriteCond %{HTTP_HOST} !^my.EXAMPLE.com [NC]

RewriteCond $1 !\.(gif|jpe?g|png|ico|css|js)$
RewriteRule ^(.*)$ http://www.EXAMPLE.com/$1 [R=301]
# Don't display .htacess files in directory listings.
IndexIgnore .htaccess
Options +FollowSymLinks
RewriteEngine on

# Password protected for now
AuthType Basic
AuthName "EXAMPLE"
AuthUserFile "/home/EXAMPLE/.htpasswds/public_html/passwd"
require valid-user

# Redirect HTTPS requests for non-SSL pages back to HTTP. (Note that shared objects
# such as images on both HTTP and HTTPS pages are excluded from this rule)
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^my\.EXAMPLE\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !\.(gif|jpe?g|png|ico|css|js)$
RewriteRule ^(.*)$ http://www.EXAMPLE.com/$1 [R=301]


# Redirect non-www requests to www
RewriteCond %{HTTP_HOST} ^EXAMPLE.com$
RewriteCond %{HTTP_HOST} !^my\.EXAMPLE\.com [NC]
RewriteRule ^(.*)$ "http\:\/\/www\.EXAMPLE\.com\/$1" [R=301]

# Prevent direct access to the WHMCS folder must be accessed through secure subdomain
RedirectMatch 301 ^/WHMCS/(.*)$ https://my.EXAMPLE.com/$1

    
ErrorDocument 404 /404.php
简言之:

if HTTPS
if not in my.example.com
if NOT an image/css/js file
redirect to HTTP
但这并没有像预期的那个样工作,相反,若我试图通过HTTPS访问my.example.com子目录之外的页面,我会得到一个404NotFound错误。通过HTTP访问同一页面没有问题,工作正常

你知道为什么这个规则不起作用吗

编辑 以下是整个.htaccess文件:

# Redirect HTTPS requests for non-SSL pages back to HTTP. (Note that shared objects
# such as images are excluded from this rule)

RewriteCond %{HTTPS} =on

# my.EXAMPLE.com is the secure subdirectory.
RewriteCond %{HTTP_HOST} !^my.EXAMPLE.com [NC]

RewriteCond $1 !\.(gif|jpe?g|png|ico|css|js)$
RewriteRule ^(.*)$ http://www.EXAMPLE.com/$1 [R=301]
# Don't display .htacess files in directory listings.
IndexIgnore .htaccess
Options +FollowSymLinks
RewriteEngine on

# Password protected for now
AuthType Basic
AuthName "EXAMPLE"
AuthUserFile "/home/EXAMPLE/.htpasswds/public_html/passwd"
require valid-user

# Redirect HTTPS requests for non-SSL pages back to HTTP. (Note that shared objects
# such as images on both HTTP and HTTPS pages are excluded from this rule)
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^my\.EXAMPLE\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !\.(gif|jpe?g|png|ico|css|js)$
RewriteRule ^(.*)$ http://www.EXAMPLE.com/$1 [R=301]


# Redirect non-www requests to www
RewriteCond %{HTTP_HOST} ^EXAMPLE.com$
RewriteCond %{HTTP_HOST} !^my\.EXAMPLE\.com [NC]
RewriteRule ^(.*)$ "http\:\/\/www\.EXAMPLE\.com\/$1" [R=301]

# Prevent direct access to the WHMCS folder must be accessed through secure subdomain
RedirectMatch 301 ^/WHMCS/(.*)$ https://my.EXAMPLE.com/$1

    
ErrorDocument 404 /404.php
试试这个:

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^my\.EXAMPLE\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !\.(gif|jpe?g|png|ico|css|js)$
RewriteRule ^(.*)$ http://www.EXAMPLE.com/$1 [R=301]

问题是,如果您没有为
www.example.com
设置SSL vhost,并且当有人转到
www.example.com
时,将其文档根指向
www.example.com
的非SSL vhost所在的位置https://www.example.com/,您的htaccess文件从未被读取。我敢打赌,您需要将HTTPS->HTTP规则放入sslvhost的htaccess文件中(其中
my.example.com
的文档根是)

你所拥有的似乎是:

http://www.example.com/ -> /home/EXAMPLE/ (or whatever your document root is)

https://my.example.com/ -> /home/EXAMPLE/subfolder/

因此,对于
https://www.example.com
,当您访问SSL EXAMPLE.com时,
/home/EXAMPLE/
中的htaccess文件永远不会被访问。

即使在删除其他重写规则并仅保留此规则后,它仍然会给我一个404错误。您的my.EXAMPLE.com和www.EXAMPLE.com是否指向同一位置?您浏览器中的链接是否从my变为www?www.example.com转到服务器上的根文件夹,“my”子域实际上指向一个子文件夹。这就解释了为什么您会得到404,
my.example.com/folder/file.php
www.example.com/folder/file.php
不是一回事,对吗,但我要做的是将my.example.com中没有的所有HTTPS请求重定向到HTTP。(http)example.com/file1.php-works,但是(https)example.com/file1.php给出了一个404错误,当它应该重定向到第一个http地址时,因为文件不在“我的”子域中。HTTPS应仅在“我的”子域内使用,并且尝试在该子域外使用HTTPS应将您重定向到您尝试访问的同一文件,只能通过http。您的htaccess文件中是否有其他规则?我将更新我的问题并包括整个htaccess文件。您要访问的URL是什么,给您一个404,还是所有内容都给您一个404?-工作正常,但会出现404错误,因为文件不在“我的”子域中,所以应该重定向到第一个HTTP地址。HTTPS只应在“我的”子域内使用,而尝试在该子域外使用HTTPS会将您重定向到您试图访问的同一文件,而只能通过http。在我的上一条评论中,第一个url前面有http://而第二个url前面有HTTPS://我必须在cPanel/WHM中四处查看,看看我在那里能找到什么,我可能能够进行vhost更改,但不确定。一旦我有时间检查它,我会让你知道它是否有效。