1. Redirect from One Page to Another
Redirect a specific page to a new page:
Redirect 301 /old-page.html http://example.com/new-page.html
2. Redirect Entire Site to a New Domain
Redirect all traffic from the old domain to the new domain:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.old-domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.new-domain.com/$1 [R=301,L]
3. Redirect All HTTP Requests to HTTPS
Redirect all HTTP requests to HTTPS:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
4. Remove www
from URL
Redirect URLs with www
to non-www
URLs:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
5. Redirect All Pages to a Maintenance Page
Redirect all pages to a maintenance page:
RewriteEngine On
RewriteRule ^(.*)$ /maintenance.html [L]
6. Redirect File Extensions (e.g., .html) to Non-Extension URLs
Redirect URLs with file extensions to URLs without extensions:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [NC,L]
7. Redirect from Non-WWW Domain to WWW Domain
Redirect non-www
domain to www
domain:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
8. Redirect All Pages from One Subdomain to Another
Redirect all pages from one subdomain to another subdomain:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub\.example\.com$ [NC]
RewriteRule ^(.*)$ http://newsub.example.com/$1 [L,R=301]
9. Redirect Specific Pages to a New URL
Redirect specific pages to new URLs:
Redirect 301 /old-page.html http://example.com/new-page.html
10. Redirect All Pages to HTTPS Domain
Redirect all pages to HTTPS domain:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
These examples can help you manage traffic and URL structure on your website effectively using .htaccess
.