Usar .htaccess for redirecciones

What is el file .htaccess?

El file .htaccess es un file de settings de Apache que vive in your folder public_html. Controla redirecciones, security, cache and muchas otras reglas of your servidor. Es invisible by default — debes activate"Mostrar files ocultos"in the Administrador de Files de cPanel for verlo.

Forzar HTTPS (redirigir HTTP → HTTPS)

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Redirigir www a without www

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

Redirigir without www a www

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Redirigir una URL específica a otra

Redirect 301 /pagina-vieja https://tudomain.com/pagina-new

Diferencia between 301 and 302

  • 301 — Permanente: Le dice a Google que la página se movió for always. Transfiere el SEO.
  • 302 — Temporal: Redirección temporal. Google mantiene la URL original en your index.
⚠️ Cuidado: Un error de sintaxis en .htaccess puede causer un Error 500 en todo your sitio. Always guarda una copia before de modificarlo.