htaccess redirect – How do I create redirect URL

0
3106
htaccess redirect - How do I create redirect URL

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.

Let’s start with discussion on redirect in htaccess.

How to Redirecting all URLs?

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/

How to Redirecting a single URL?

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.

How to Redirect to a local site file?

Redirect /path/to/old/file/old.html /path/to/new/file/new.html

How to Redirect to an external site file?

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.

Below are the example for redirects.

Redirecting from a directory to an HTML file?

RedirectMatch 301 ^/blog/about /blog/about.html

Redirect from an index.html file to a different directory?

Redirect /index.html /new/

Redirecting from index.html to default.html?

Redirect /index.html /default.html

Redirect a local /private directory to another site’s private directory?

Redirect /private/ http://www.domain-name.com/private/

Load a .gif file from a different site?

Redirect /img/logo.gif http://www.domain-name.com/images/logo.gif

How to redirect Using Regular Expressions?

If you want to use a Regular Expression to redirect something, use the RedirectMatch directive

RedirectMatch "^/oldfile\.html/?$" "http://domian-name.com/newfile.php"

How to Redirecting error messages?

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/

How to Redirecting an old directory to new directory?

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]

How to Redirect non-existing pages to index.php?

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} !-d
RewriteRule . /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.

How to Automatically loading a subdirectory?

With the help of code snippet on can easily automatically loading a subdirectory

RewriteEngine on
RewriteRule ^$ /subdir1/ [L]

Forcing www in the URL.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain-name.com
RewriteRule (.*) http://www.domain-name.com/$1 [R=301,L]

Removing www in the URL?

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.domain-name.com
RewriteRule (.*) http://domain-name.com/$1 [R=301,L]

Rewriting a URL?

Below code snippet will rewrites domain-name.com/1.html to domain-name.com/abc.php?id=1

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([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.

How to Rewriting non-existing links to index.php?

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 On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /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.