Best way to redirect a url (LAMP on debian)

hello, I've done some searching but haven't found anything that really answers my question.

I have the domains, let's say: mydomain.com, mydomain.net, mydomain.co etc. mydomain.com is my main website. What would be the best way to redirect the .net and .co domains to mydomain.com with as little interference as possible and without the user being interrupted. I do not want, for example, mydomain.net to serve the same content as mydomain.com but still show the .net domain in the URL bar, but I want it to actually redirect to the .com

I am running LAMP on debian.

Thanks

4 Replies

What I've done with my domains is have a vhost setup for my main domain pointing at one directory, and the others "children" domains point at another directory, which has an index.php file that just 301 redirects to the main site. Might not be the nicest way to do it but I've read it doesnt hurt your SEO stats like other methods.

Good luck

Enable mod_rewrite and have something like this in your vhost configuration file (in /etc/apache2/sites-available):

ServerName mydomain.com
ServerAlias www.mydomain.com
ServerAlias mydomain.net
ServerAlias www.mydomain.net

RewriteEngine on
RewriteCond %{HTTP_HOST} !^mydomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://mydomain.com/$1 [L,R=301]

Another method is to simply use Apache's RedirectMatch:

 <virtualhost *:80="">ServerName mydomain.net
ServerAlias mydomain.co
RedirectMatch permanent ^(.*)$ http://mydomain.com$1</virtualhost> 

 <virtualhost *:80="">ServerName mydomain.com
# Complete configuration here...</virtualhost> 

Would redirect all requests for mydomain.net and mydomain.co to mydomain.com

@sleddog:

Another method is to simply use Apache's RedirectMatch:

 <virtualhost *:80="">ServerName mydomain.net
ServerAlias mydomain.co
RedirectMatch permanent ^(.*)$ http://mydomain.com$1</virtualhost> 

 <virtualhost *:80="">ServerName mydomain.com
# Complete configuration here...</virtualhost> 

Would redirect all requests for mydomain.net and mydomain.co to mydomain.com

Thanks, I like that approach much better as I dont even need to have a separate directory structure for my extra domains, and my vhost file is much cleaner.

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct