Jo Colina
Jo Colina

Reputation: 1924

Apache redirect from root

I was just wondering how to perform a redirect from root in Apache.

I want to check if someone goes to the root url (eg. example.com) and redirect them to example.com/h automatically.

Can I do this in apache config, or in a .htaccess file?

Upvotes: 5

Views: 582

Answers (3)

Suriyanarayanan
Suriyanarayanan

Reputation: 61

NameVirtualHost *:80

<VirtualHost *:80>
        ServerName example.com
        RedirectMatch 301 ^/$ /h
</VirtualHost>

This will help you to redirect all your request from example.com to example.com/h

Upvotes: 5

anubhava
anubhava

Reputation: 785146

You can use this mod_rewrite rule in Apache config or in root .htaccess:

RewriteEngine On

RewriteRule ^/?$ /h [L,R=302]

Change R=302 to R=301 after verifying this redirect rule. If you don't want the URL in the browser to change then use:

RewriteRule ^/?$ /h [L]

Upvotes: 2

MrWhite
MrWhite

Reputation: 45829

You can do this in either Apache config or .htaccess (per directory config files).

Using mod_rewrite in .htaccess, to redirect from the root to a specific URL:

Options +FollowSymLinks
RewriteEngine On

RewriteRule ^$ /h [R,L]

Note that this is a temporary (302) redirect.

Upvotes: 0

Related Questions