【问题标题】:how to force website to open with www and https with htaccess如何使用 htaccess 强制使用 www 和 https 打开网站
【发布时间】:2018-05-09 08:05:57
【问题描述】:

我无法将我的 example.in 指向 https://www.example.in 我正在使用 .htaccess

RewriteEngine On
RewriteCond %{SERVER_PORT} 80

RewriteCond %{HTTPS} off
# First rewrite to HTTPS:
# Don't put www. here. If it is already there it will be included, if 
not
# the subsequent rule will catch it.
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Now, rewrite any request to the wrong domain to use www.
# [NC] is a case-insensitive match
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]


# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]
AcceptPathInfo On

我在 stackoverflow 上找到了这段代码,但它在我的项目中不起作用 我的网站在 AWS 上并使用 apache2 php 7.0 的虚拟主机

它不是强制 www 并且我已经尝试了其他代码 .htaccess 但如果我从 url 手动删除 www 它不会重定向到 www 。

【问题讨论】:

  • # 不要放 www。这里。如果它已经存在,它将被包括在内,如果没有 - 这是在同一行吗?它看起来像一个错字。
  • 是的,它在同一行
  • 我没有正确阅读您的问题。请检查:stackoverflow.com/a/17458396/6811933
  • 我试过了,但它不起作用......早期代码的问题就像我去“http://domain.in”=>“https://www.domain.in”时一样......这很好....但是如果我在这种情况下转到“https://domain.in”=>“https://domain.in”,则网址重定向不会强制它添加 www
  • 如果我要去http://domain.in/profile/,它既不会重定向https,也不会重定向www

标签: .htaccess redirect apache2


【解决方案1】:

感谢大家的支持,

我已经解决了我的问题,因为ssl 安装在我的网站上,因此安装在/etc/apache2/sites-available/

我找到了 default-ssl.conf 文件,其中包含一些设置,我猜我不允许我使用 .htaccess 进行 ovveride。

但现在添加后

<Directory /var/www/myfolder > AllowOverride All </Directory>

htaccess 工作正常。

【讨论】:

    猜你喜欢
    • 2013-05-31
    • 1970-01-01
    • 1970-01-01
    • 2014-01-20
    • 2016-04-05
    • 2019-01-27
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    相关资源
    最近更新 更多