HTACCESSファイルでのWWWリダイレクト

May 29 2020

Reactアプリのhtaccessファイルがあります https://searchglutenfree.com/。自動的に書き換えて欲しいhttps://www.searchglutenfree.com/ に https://searchglutenfree.com/ リダイレクト中にすべてのパラメータを保持しながら。

この素晴らしいデフォルトのhtaccessテンプレートをGitHubで見つけました(https://gist.github.com/iheartmedia-matt/253ccb6183fdeaa5619f615f2cb5a58b)、そして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>

回答

2 CBroe Jun 09 2020 at 11:36

これをホスト名に関して動的にする必要がない場合は、ホスト名がをwww.チェックするものの後に始まるかどうかをチェックする条件を%{HTTPS} off追加[OR]し、前者にフラグを追加します-そして単にハードコーディングします次のルールの置換URLのホスト名。

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

コメント# If its not HTTPS# If we get to here, it means we are on https://上記の.htaccess内のすべてをそれで置き換えると、機能するはずです。

ArmandoBallaci Jun 09 2020 at 11:27

www.example.comからexample.comへ、またはその逆のリダイレクトを設定するには、名前ごとにAレコードが必要です。

ユーザーをwwwからwww以外のプレーンなドメインにリダイレクトするには、次の構成を挿入します。

あなたの.htaccessで:

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