Apache redirect examples using mod_alias

Hi! here I will provide few samples of how to create redirect in Apache web server. You can test this examples locally in your localhost.

First example

In the first example let’s redirect to Google search when we trying to go to localhost.

1. Go to the root folder of your web server. In my case, it’s C:\xampp\htdocs\.htaccess and create .htaccess file if it doesn’t exist.

2. At the first line of .htaccess write:

Redirect / http://google.com

4. Try to go to your localhost again. Apache will redirect you to Google search.

Second example

In this example le’ts redirect to Google search only when somebody trying to go to a web server folder “my”.

1. At the first line of .htaccess write:

Redirect /my/ http://google.com

2. Now, when you go to your localhost you will get the right localhost page, but when you try to go to http://localhost/my/ it redirects you to Google search.

Third example

1. Let’s redirect to a subfolder when querying a subfolder. For instance, when the user entering http://localhost/my/ let’s redirect to http://localhost/site/ or when the user querying http://localhost/my/page.html let’s redirect to http://localhost/site/page.html

2. Edit your .htaccess:

Redirect /my/ /site/

mod_alias

All these tricks are done with help of mod_alias so this module has to be turned on. You can read more abous this module at Apache:

2 Responses to “Apache redirect examples using mod_alias”