如何强制所有流量使用HTTPS?

3
我正在使用.htaccess将所有流量重定向到https。我正在使用此答案中的示例:需要将所有流量重定向到https
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

这个对我没有任何重定向的作用。我把它放在了var/www,但似乎没有任何效果。我哪里做错了?


请检查您是否启用了 mod_rewrite - vee
在 .htaccess 文件的顶部添加一些垃圾测试内容,看看是否会出现 500 错误。 - anubhava
Stack Overflow是一个关于编程和开发问题的网站。这个问题似乎不属于编程或开发范畴。请参阅帮助中心中的我可以在这里问什么样的问题。也许超级用户Unix&Linux Stack Exchange更适合提问。另请参阅我应该在哪里发布有关Dev Ops的问题? - jww
2个回答

4

3

如果这是你的htaccess文件中唯一的规则,并且它在你的文档根目录下,那么你需要检查几件事情,因为这些规则是正确的。

  1. Make sure mod_rewrite is loaded. There should be a line in your httpd.conf file that looks something like:

    LoadModule rewrite_module modules/mod_rewrite.so
    

    Make sure it's uncommented.

  2. Make sure the directory that your htaccess file is in (should be your document root) is allowed to override server settings via htaccess. In your vhost or server config, there should be something along the lines of

    <Directory "/var/www/">
        AllowOverride All
    
        ... (some other stuff)
    </Directory>
    

    Make sure the AllowOverride is at least FileInfo

  3. Make sure your document root is actually where your htaccess file is in. Your vhost config should have a line like:

    DocumentRoot /var/www/
    
  4. Make sure the document root is for the right vhost. If you have separate vhosts for SSL and non-SSL, make sure the htaccess file is in the document root for the non-SSL vhost.


我对服务器管理很新,并且正在学习中。我在Digital Ocean虚拟服务器上运行Ubuntu。我现在会去检查所有这些... - Paul Dessert
我的 httpd.conf 文件是空的,Ubuntu 配置不同吗? - Paul Dessert
@relentless 对于Ubuntu,请参见此答案 - Jon Lin
谢谢。我检查了你指出的所有问题。AllowOverride被设置为none,而我将其更改为All,重新启动Apache,但我仍然遇到问题。 - Paul Dessert

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接