WWW-Umleitung in einer HTACCESS-Datei

May 29 2020

Ich habe eine htaccess-Datei für eine React-App unter https://searchglutenfree.com/. Ich möchte, dass es automatisch neu geschrieben wirdhttps://www.searchglutenfree.com/ zu https://searchglutenfree.com/ während alle Parameter während der Umleitung beibehalten werden.

Ich habe diese großartige Standard-htaccess-Vorlage auf GitHub gefunden (https://gist.github.com/iheartmedia-matt/253ccb6183fdeaa5619f615f2cb5a58b), und das WWW umzuleiten ist das Letzte, was ich brauche. Weiß jemand, was ich hinzufügen muss und wo in der Datei, um das WWW neu zu schreiben?

<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>

Antworten

2 CBroe Jun 09 2020 at 11:36

Wenn Sie dies nicht benötigen, um hinsichtlich des Hostnamens dynamisch zu sein, würde ich eine Bedingung hinzufügen, die prüft, ob der Hostname www.nach demjenigen beginnt %{HTTPS} off, nach dem gesucht wird , und das [OR]Flag dem ersteren hinzufügen - und dann einfach hart codieren Der Hostname in der Ersetzungs-URL der folgenden Regel.

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

Wenn Sie alles zwischen den Kommentaren # If its not HTTPSund # If we get to here, it means we are on https://in Ihrem oben gezeigten .htaccess damit ersetzen , sollte es funktionieren.

ArmandoBallaci Jun 09 2020 at 11:27

Um die gewünschte Weiterleitung von www.example.com zu example.com oder umgekehrt einzurichten, müssen Sie für jeden Namen einen A-Datensatz haben.

Fügen Sie diese Konfiguration ein, um Benutzer von www zu einer einfachen Nicht-www-Domäne umzuleiten:

in Ihrem .htaccess:

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