Question

I have a domain that doesn't have a website, but instead I would like to use that domain to forward to url's where I do have websites. Is the .htaccess file the way to go?

For example I have the domain example.com and want to forward differently based on the subdirectory:

www.example.com --> www.test.com

www.example.com/membership --> www.test.com/membership

Was it helpful?

Solution

If you are using Apache, you can have this information stored directly in the server configuration.

Documentation here: http://httpd.apache.org/docs/current/rewrite/remapping.html

You should be able to use any of the examples above directly in a .htaccess file too if that is your preferred method.

It might be better to implement your redirect in the server configuration if it is a permanent move, however a .htaccess file should be fine.

Edit: I've added code to add to a .htaccess , the first redirects all traffic to the new domain, the second redirects to the new domain whilst maintaining subdirectory paths.

Redirect All:

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/ [R=301,L]

Maintain Subdirectory Paths:

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]

OTHER TIPS

You can bootstrap everything to index.php?path=$1 using mod_rewrite and make intelligent decisions there using header('Location: <URL>'); in case you need to change it later or you have some very specific requirements.

Otherwise, .htaccess should be ok. (probably better for simple redirections)

Yes you can use .htaccess if you have a server with Apache installed.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.example.com[nc]
RewriteRule ^(.*)$ http://www.test.com/$1 [r=301]

You can also do in similar way on other web servers like nginx or lighttpd.

Rewrite rules are generated from here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top