Nội dung
If you are trying to redirect one or more of your website’s URLs, this article about redirecting with .htaccess will be very helpful.
Note: If you use WordPress, some redirects won’t work because it will use its guidelines by default. And the solution for WordPress is that you should use the Redirection plugin.
I. What is a .htaccess file?
.htaccess is a configuration file for use on supported Web Servers such as Apache, LiteSpeed, OpenLiteSpeed…
When the .htaccess file is placed in a website source directory, the .htaccess file is executed by the Web Server. These .htaccess files can be used to change the configuration of the Web Server software to enable/disable additional functionality and features that the Web Server software provides.
II. Aggregate redirects with .htaccess file
The examples below can be added to the file .htaccess depending on your needs. Just be sure to adjust the actual path to the file, so the .htaccess file knows where it is. In addition, if you see the domain example.com , change this to your domain.
1. Redirect all URLs
The following configuration line will redirect all the URLs on your current site to the new site:
Redirect 301 / https://example.com/
2. Redirect to a URL
Using Redirect in a .htaccess file allows you to redirect user traffic from the old site to the new one without keeping the old site.
Example: If you use index.html
as your index file and then rename index.html
to home.html
, you can set up a redirect to take the user from index.html
to home.html
with the fragment following code:
Redirect /path/to/old/file/index.html /path/to/new/file/home.html
If you want to redirect to another website’s home.html
file then use this code:
Redirect /path/to/old/file/index.html https://www.example.com/new/file/home.html
3. Some examples of redirects using .htaccess
Redirect from a directory to an HTML file:
RedirectMatch 301 ^/blog/about /blog/about.html
Redirect the index.html
file to another directory:
Redirect /index.html /new/
Redirect from index.html
file to default.html
file:
Redirect /index.html /default.html
Redirect an internal /private
directory to another site’s /private
directory:
Redirect /private/ https://www.example.com/private/
Load a .gif
file from another site:
Redirect /img/logo.gif https://www.example.com/images/logo.gif
Redirect 404 error messages back to website homepage:
ErrorDocument 404 https://example.com/
Redirect an old directory to a new directory:
Redirect files in the old directory /blog/archives
to a new directory /archives
.
RewriteRule ^blog/archives/(.*)$ /newarchives/\ [R=301,NC,L]
4. Redirect non-existent pages to index.php
If a visitor tries to access a page that doesn’t exist, they’ll get a 404 error. Instead, you can redirect any request to a non-existent page to your index.php
file (or whatever any other index files) by adding the following code to your .htaccess
:
Options +SymLinksIfOwnerMatch RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L]
If your index page is not index.php
but index.html or another, just change the last line to your actual index file. The visitor is then redirected back to your homepage.
5. Redirect the main directory to a subdirectory
For example, redirect the URL of the main directory to any subdirectories. In this example, it automatically loads example.com/subdir1
when you type example.com
in the browser:
RewriteEngine on RewriteRule ^$ /subdir1/ [L]
6. Redirect to www
RewriteEngine On RewriteCond %{HTTP_HOST} ^example.com RewriteRule (.*) https://www.example.com/\ [R=301,L]
7. Remove www in URL
RewriteEngine On RewriteCond %{HTTP_HOST} ^www.example.com RewriteRule (.*) https://example.com/\ [R=301,L]
8. Rewrite one URL to another
This example rewrites one URL to another. It will rewrite example.com/1.html
to example.com/abc.php?id=1
.
Options +FollowSymLinks RewriteEngine On RewriteRule ^([0-9]+).html /abc.php?id=$1 [QSA,L]
9. Rewrite non-existent links back to the homepage
Redirect all links to non-existent files or directories to index.php
. However, if the file or path does exist, it will be accessible normally:
RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L]
10. Redirect http to https
RewriteEngine on RewriteCond %{HTTPS} !=on [NC] RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUESTURI} [R=301,L]
III. Summary
AZDIGI hopes these .htaccess redirect examples will help you to solve your problems. Wishing you success!
See more useful website tutorial articles at the following link:
If you need assistance, you can contact support in the ways below:
- Hotline 247: 028 888 24768 (Ext 0)
- Ticket/Email: You use the email to register for the service and send it directly to: support@azdigi.com .
- Click on the AZDIGI website to refer to the best hosting/VPS service in Vietnam.