【问题标题】:How do I edit apache .htaccess correctly?如何正确编辑 apache .htaccess?
【发布时间】:2021-04-21 19:17:05
【问题描述】:

美好的一天!我想使用 .htaccess 文件实现 3 个任务:

  1. 仅适用于https 协议
  2. 创建从 https://www.example.comhttps://example.com 的 301 重定向
  3. 将所有请求重定向到index.php 文件

我最好的 .htaccess 版本如下所示,但我不确定这是否是正确的决定:

RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{HTTP_HOST} .* [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php

如有任何建议,我将不胜感激!

【问题讨论】:

    标签: apache .htaccess mod-rewrite https url-rewriting


    【解决方案1】:

    您能否尝试按照您显示的示例编写以下内容。请确保在测试 URL 之前清除浏览器缓存。

    RewriteEngine ON
    ##For applying https on each request.
    RewriteCond https !on
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,NE,L]
    
    ##For removing www from request.
    RewriteCond %{HTTP_HOST} ^(?:www\.)(.*)$ [NC]
    RewriteRule ^ https://%1%{REQUEST_URI} [R=301,NE,L]
    
    ##For applying index.php for non existing files or directories.
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^ index.php [L]
    

    【讨论】:

      猜你喜欢
      • 2016-05-15
      • 2012-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-03
      • 1970-01-01
      • 2017-03-12
      相关资源
      最近更新 更多