In this article, you going can see discussion regarding htaccess redirect or rewrite with step by step in detail.
If you looking for this topic then you are at right place.
If you want to redirect all the URLs use the below code snippet in htaccess file are as follow as:
Redirect 301 / https://domain-name.com/
With the help of Redirect in an .htaccess
file helps you to redirect users from an old to a new page without having to keep the old page.
Below is example for redirecting a single Url that from old to new.
Redirect /path/to/old/file/old.html /path/to/new/file/new.html
Redirect /path/to/old/file/old.html http://www.domain-name.com/new/file/new.html
If you check in the above redirect to a local site file, there is old file must be a local UNIX path which not be the full path.
And you cannot include /home/exampleuser/domain-name.com
in the local UNIX path, if it is inside directory /domain-name.com
The first /
represents the domain-name.com directory.
For the old file name you have to follow the /
, it is because if the old file in that directory.
In the above second path for Redirect to an external site file if you check path to new file can be a local UNIX path.
It can also be a full URL to link to a page on a different server or it can also be a the same server.
RedirectMatch 301 ^/blog/about /blog/about.html
Redirect /index.html /new/
Redirect /index.html /default.html
Redirect /private/ http://www.domain-name.com/private/
Redirect /img/logo.gif http://www.domain-name.com/images/logo.gif
If you want to use a Regular Expression to redirect something, use the RedirectMatch
directive
RedirectMatch "^/oldfile\.html/?$" "http://domian-name.com/newfile.php"
To redirect 404 errors. Instead of throwing a 404 page, this redirects to the homepage of the website, below is code snippet for it.
ErrorDocument 404 http://domain-name.com/
It helps in redirecting files in a old directory (/blog/archives)
to a new directory (/archives)
.
RewriteRule ^blog/archives/(.*)$ /newarchives/$1 [R=301,NC,L]
One of best method Redirect to non-existing pages if a visitor attempts to access a page that doesn’t exist, they are presented with a 404 error.
In that case, you can instead redirect any request to a non-existing page to your index.php
file (or any index file) by adding the following code in your .htaccess
Options +SymLinksIfOwnerMatch RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-dRewriteRule . /index.php [L]
And the main point is that If your index page isn’t index.php
, just change the last line to your actual index file.
Due to this, then the visitor is redirected back to your home page.
With the help of code snippet on can easily automatically loading a subdirectory
RewriteEngine onRewriteRule ^$ /subdir1/ [L]
RewriteEngine OnRewriteCond %{HTTP_HOST} ^domain-name.comRewriteRule (.*) http://www.domain-name.com/$1 [R=301,L]
RewriteEngine OnRewriteCond %{HTTP_HOST} ^www.domain-name.comRewriteRule (.*) http://domain-name.com/$1 [R=301,L]
Below code snippet will rewrites domain-name.com/1.html to domain-name.com/abc.php?id=1
Options +FollowSymLinksRewriteEngine OnRewriteRule ^([0-9]+).html /abc.php?id=$1 [QSA,L]
Below are the rules for above:
([0-9]+):
It allows any digit, and only any digit, 1 or more times.
([a-z-])
: It allows any lowercase letter, plus "-"
for word separation, 0 or more times. If you want it to support uppercase too, use "([a-zA-Z-])
.
[QSA,L]
: It helps in appending to your internal scripting query string and makes it the Last rewrite rule executed.
With the help of this you can retrieve the webpage with either address type.
Below code snippet helps in redirects all links to files or folders that do not exist to index.php
However, if the file or directory does exist, it loads normally:
<IfModule mod_rewrite.c>RewriteEngine OnRewriteBase /RewriteRule ^index\.php$ - [L]RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule . /index.php [L]</IfModule>
Conclusion:
Finally, we have done regarding all points of htaccess redirect.
I hope you like this article and if you have any query, please comment below.