【问题标题】:WWW Redirection in an HTACCESS FileHTACCESS 文件中的 WWW 重定向
【发布时间】:2020-09-17 02:27:59
【问题描述】:

我在https://searchglutenfree.com/ 有一个 React 应用程序的 htaccess 文件。我希望它自动将https://www.searchglutenfree.com/ 重写为https://searchglutenfree.com/,同时在重定向期间保留所有参数。

我在 GitHub (https://gist.github.com/iheartmedia-matt/253ccb6183fdeaa5619f615f2cb5a58b) 上找到了这个很棒的默认 htaccess 模板,并且让 www 重定向是我最不需要的。有谁知道我需要添加什么以及在文件中的哪个位置进行 WWW 重写?

<ifModule mod_rewrite.c>


  #######################################################################
  # GENERAL                                                             #
  #######################################################################

  # Make apache follow sym links to files
  Options +FollowSymLinks
  # If somebody opens a folder, hide all files from the resulting folder list
  IndexIgnore */*


  #######################################################################
  # REWRITING                                                           #
  #######################################################################

  # Enable rewriting
  RewriteEngine On

  # If its not HTTPS
  RewriteCond %{HTTPS} off

  # Comment out the RewriteCond above, and uncomment the RewriteCond below if you're using a load balancer (e.g. CloudFlare) for SSL
  # RewriteCond %{HTTP:X-Forwarded-Proto} !https

  # Redirect to the same URL with https://, ignoring all further rules if this one is in effect
  RewriteRule ^(.*) https://%{HTTP_HOST}/$1 [R,L]

  # If we get to here, it means we are on https://

  # If the file with the specified name in the browser doesn't exist
  RewriteCond %{REQUEST_FILENAME} !-f

  # and the directory with the specified name in the browser doesn't exist
  RewriteCond %{REQUEST_FILENAME} !-d

  # and we are not opening the root already (otherwise we get a redirect loop)
  RewriteCond %{REQUEST_FILENAME} !\/$

  # Rewrite all requests to the root
  RewriteRule ^(.*) /

</ifModule>

<IfModule mod_headers.c>
  # Do not cache sw.js, required for offline-first updates.
  <FilesMatch "sw\.js$">
    Header set Cache-Control "private, no-cache, no-store, proxy-revalidate, no-transform"
    Header set Pragma "no-cache"
  </FilesMatch>
</IfModule>

【问题讨论】:

  • 见:https://stackoverflow.com/questions/6515081/htaccess-remove-www-from-url-directories
  • 感谢@hacker315,我实际上在文件的几个不同位置插入了这段代码,但它对我不起作用。也许我错过了什么?
  • 如果您不需要这对于主机名是动态的,那么我会在检查%{HTTPS} off 之后添加一个检查主机名是否以www. 开头的条件,并将[OR] 标志添加到前者 - 然后只需在以下规则的替换 URL 中硬编码主机名。
  • 谢谢@CBroe。我对 htaccess 真的很不好,所以我要找的是确切的代码。我有一个赏金所以如果你回答确切的变化并且它有效,我会奖励你:)
  • 添加了一个答案,基本上应该这样工作。如果您在我提到的 cmets 之后保留其余的重写,那么一切都应该正常工作。 (在这里说应该,因为这些事情并不总是很容易获得 100% 正确,无法在实际系统上进行测试。如果您遇到任何问题,请告诉我。)

标签: .htaccess


【解决方案1】:

如果您不需要这对于主机名是动态的,那么我将添加一个条件来检查主机名是否以 www. 在检查 %{HTTPS} off 之后,然后添加 @987654324 @ 标记到前者 - 然后简单地将主机名硬编码在以下规则的替换 URL 中。

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*) https://searchglutenfree.com/$1 [R,L]

如果您将上面显示的 .htaccess 中 cmets # If its not HTTPS# If we get to here, it means we are on https:// 之间的所有内容替换为上面显示的内容,它应该可以工作。

【讨论】:

  • R 标志本身将导致状态码为 302 的临时重定向,这有利于测试(否则您的浏览器将自己缓存重定向,这样很容易欺骗您,当您进行更改后看不到任何效果。)如果一切正常,请将其替换为 R=301 以使其成为永久重定向。
  • 现有的重定向有哪些?
  • (整个事情现在应该是这个样子,pastebin.com/U01Af425
  • 删除了我的最后一条评论。这是99%!有没有办法删除“seachglutenfree.com”并使其成为通配符?我必须将其复制到几个站点中,并且不必替换该部分会很好。
  • 发现另一个问题... https://www.searchglutenfree.com 没有正确重定向(没有空格,添加以显示不起作用的 URL)
【解决方案2】:

为了设置所需的重定向,从 www.example.com 到 example.com,反之亦然,每个名称都必须有一个 A 记录。

要将用户从 www 重定向到普通的非 www 域,请插入以下配置:

在您的 .htaccess 中:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

【讨论】:

  • 我在第 24 行尝试了这段代码,就像上面的 htaccess 文件的第 36 行一样。在第 26 行,它将正确重定向 searchglutenfree.com,但不会重定向 htttps://www.searchglutenfree.com。这段代码应该插入到哪一行?如果你能告诉我这适用于哪一行,我很乐意给你赏金。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-08
  • 2019-01-29
  • 2011-04-12
  • 2015-12-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多