2012-08-31 61 views
0

我想确保如果HTTPS在安全子域之外使用,它将被重定向到HTTP。htaccess重写规则中的错误

这是我的根.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] 

简而言之:

if HTTPS 
if not in my.example.com 
if NOT an image/css/js file 
redirect to HTTP 

但预期这是行不通的,而不是如果我尝试外部访问页面通过HTTPS my.example.com子目录的我得到一个404 Not Found错误。通过HTTP访问相同的页面没有问题,它工作正常。

任何想法为什么这个规则可能不工作?

编辑

这里就是整个.htaccess文件:

# 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 
+0

你对htaccess文件有其他规则吗? –

+0

我会更新我的问题,并包括整个htaccess文件 –

+0

你打算给你的网址是404还是一切都会给你404? –

回答

0

试试这个:

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] 
+0

它仍然给了我一个404错误,即使删除其他重写规则,只是离开这一个。 –

+0

您的my.example.com和www.EXAMPLE.com指向同一位置吗?和你的浏览器中的链接是否从我更改为www? – Oussama

+0

www.example.com转到服务器上的根文件夹,'my'子域实际上指向一个子文件夹。 –

0

的问题是,如果你没有一个SSL虚拟主机设置为www.example.com,并将它的文档根指向www.example.com的非SSL虚拟主机所在的位置,当有人访问https://www.example.com/时,您的htaccess文件从不读取。我敢打赌,您需要将HTTPS-> HTTP规则放入您的SSL虚拟主机的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,在/home/EXAMPLE/ htaccess的文件从来没有当你去到SSL example.com访问。

+0

我必须在cPanel/WHM中查看我可以在其中找到的内容,或许可以更改虚拟主机,但不确定。 一旦我有时间检查它,我会让你知道它是否工作。 –