Here are 10 examples of using redirects in an .htaccess file

1. Redirect from One Page to Another

Redirect a specific page to a new page:

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

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

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

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

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

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

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

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

apache
Redirect 301 /old-page.html http://example.com/new-page.html

10. Redirect All Pages to HTTPS Domain

Redirect all pages to HTTPS domain:

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

  • хостинг, htaccess, редирект 301
  • 2 Users Found This Useful
Was this answer helpful?

Related Articles

HTTP Errors: common causes and how to fix them

Error 403: ForbiddenDescription: The server understands the request but refuses to fulfill it....

Configuring DNS on Cloudflare

Cloudflare is a popular service that provides protection and performance improvements for...

Connecting to hosting via FTP using FileZilla

FileZilla is a simple and convenient tool for managing files on your server. Here’s a...

What is a subdomain and how is it used?

A subdomain is a part of a domain name that is added before the main domain name. It is used to...

Testing network speed on a VPS using Speedtest

To test the network speed on your VPS running Debian or AlmaLinux, you can use the speedtest-cli...

Powered by WHMCompleteSolution