apache - htaccess - 在子文件夹中使用域强制 SSL 和 www

标签 apache .htaccess ssl mod-rewrite cpanel

我正在使用 cPanel 主机,不幸的是,它不允许我访问 Apache conf 文件以设置任何 VirtualHosts。我的域在子文件夹中。我现在也在尝试强制使用 SSL 以及强制使用 www

我的要求:

  • 域需要在子文件夹中,因为我还有其他域
  • 强制使用 SSL
  • 强制 www.在网址中

以下是我的 .htaccess 文件中的内容:

RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subfolder/$1
RewriteCond %{HTTP_HOST} ^(www.)example.com$
RewriteRule ^(/)?$ subfolder/index.php [L]

我进行了各种尝试,似乎满足了我的一两个要求,但并非同时满足所有要求。

感谢任何帮助。

最佳答案

我快到了。在第二行之后,我必须添加几个 RewriteConds。不在 HTTPS 上,也不在 www 上:

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]

最终的规则集:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,L,R]
RewriteRule ^$ /subfolder/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subfolder/$1 [L]

经过测试,它在域中强制使用 SSL 和 WWW,并将所有内容保留在子文件夹中。

关于apache - htaccess - 在子文件夹中使用域强制 SSL 和 www,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51332858/

相关文章:

css - Google 字体在 Firefox 中不起作用

apache - apache ubuntu16.0.4上的ssl安装

android - CertPathValidatorException : Trust anchor for certificate path not found - Retrofit Android

c++ - 以PHP方式使用C++的Web开发环境

apache - 如何使用 htaccess 对所有 *.html 查询返回 410 错误?

apache - mod_headers 模块不加载,尽管它在 httpd.conf 中启用

php - 直接加载时显示空白页 (PHP)

php - root@localhost 的访问被拒绝 MYSQL 错误

Apache 上的 Django 无效的 HTTP_HOST header - 使用 Require 修复?

旧版 Qt 4.2.3 中的 SSL 实现