Redirección WWW en un archivo HTACCESS

May 29 2020

Tengo un archivo htaccess para una aplicación React en https://searchglutenfree.com/. Quiero que se reescriba automáticamentehttps://www.searchglutenfree.com/ a https://searchglutenfree.com/ manteniendo todos los parámetros durante la redirección.

Encontré esta excelente plantilla htaccess predeterminada en GitHub (https://gist.github.com/iheartmedia-matt/253ccb6183fdeaa5619f615f2cb5a58b), y conseguir que www se redireccione es lo último que necesito. ¿Alguien sabe lo que necesito agregar y en qué lugar del archivo para obtener la reescritura de 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>

Respuestas

2 CBroe Jun 09 2020 at 11:36

Si no necesita que esto sea dinámico con respecto al nombre de host, agregaría una Condición que verifique si el nombre de host comenzó www.después del que busca %{HTTPS} off, y agregaría la [OR]bandera al primero, y luego simplemente codificará el nombre de host en la URL de sustitución de la siguiente regla.

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

Si reemplaza todo entre los comentarios # If its not HTTPSy # If we get to here, it means we are on https://en su .htaccess que había mostrado anteriormente con eso, debería funcionar.

ArmandoBallaci Jun 09 2020 at 11:27

Para configurar el redireccionamiento deseado, de www.example.com a example.com o viceversa, debe tener un registro A para cada nombre.

Para redirigir a los usuarios de www a un dominio sin www, inserte esta configuración:

en tu .htaccess:

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