Need a new web host or having a problem with a script? Tips and advice on the best way to design, build and maintain your most valuable asset - your website!

Redirecting domains - Done through a .htaccess file?

crowzy
Posts: 40
Joined: 15 Dec 06
Trust:

Redirecting domains - Done through a .htaccess file?

Hi everyone,

I want to redirect [http://www.domain.com/index.html] and [http://domain.com] to [http://www.domain.com]

My understanding is that this should be done through .htaccess file.
What code should I use to do this and is there anything else that should be redirected? (For example, is [http://www.domain.com] also considered as a different domain in the eyes of SE's.)

And 2 more, probably silly, questions but I just want to be sure.

1. .htaccess file already exists under my public_html folder and it is empty at the moment. Do I just type the code straight into it through my control panel or does it have to be done in NotePad first and then uploaded?

2. I also want to do exactly the same thing for an addon domain. Do I use the same .htaccess file or a new one under public_html/addon.com folder should be created?

Thanks in advance for your help.

Cheers
  • 0
PremiumMember
chatyak86
Posts: 1085
Joined: 17 Jun 06
Trust:
In your blank .htaccess file put this...


redirect 301 /index.html [http://www.newdomain.com]


I'm not sure you can redirect a [http://domain.com] since it's default is index.html anyways.
  • 0
Site Admin
markling
Posts: 2071
Joined: 13 Jun 06
Trust:
For your addon domain you can create a separate .htaccess file inside that domain's folder.

You'll be fine either editing it through cpanel (yup, just enter the text. Each new command goes on a separate line) or in notepad. If you're creating it in notepad and it won't let you save a file with no file name (only an extension) you can just call it blah.txt and then rename it to .htaccess once you've uploaded it.

All the best,
Mark
  • 0
Limited time special - Pathway to Passive for $37: https://www.affilorama.com/pathwaytopassive
 
crowzy
Posts: 40
Joined: 15 Dec 06
Trust:
Hi Adrian,

Thanks for the reply. I have actually seen the code for both of them but I cannot recall where it was.

I am sure the code was much longer and more complicated. Also some $ signs were included.
  • 0
crowzy
Posts: 40
Joined: 15 Dec 06
Trust:
Thank you Mark.

So I do need a separate .htaccess file for every new addon?

And how about the code. Do you know anything about the one I mentioned (with $ sings in it)?
  • 0
PremiumMember
chatyak86
Posts: 1085
Joined: 17 Jun 06
Trust:
crowzy wrote:Thank you Mark.

So I do need a separate .htaccess file for every new addon?

And how about the code. Do you know anything about the one I mentioned (with $ sings in it)?


I have not seen dollar signs in a 301 redirect .... they are mostly used in PHP... but I could be wrong.
  • 0
crowzy
Posts: 40
Joined: 15 Dec 06
Trust:
OK. I've found the code. Could anyone please let me know is this the right way to do it?

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ [http://www.domain.com/$1] [R=301]
RewriteRule ^index.html$ [http://www.domain.com] [R=301,L]

Thanks.
  • 0
crowzy
Posts: 40
Joined: 15 Dec 06
Trust:
NO?
  • 0
PremiumMember
chatyak86
Posts: 1085
Joined: 17 Jun 06
Trust:
I personally wouldn't use that code crowzy. The one I gave you is a simple and clean code to use.
  • 0
crowzy
Posts: 40
Joined: 15 Dec 06
Trust:
Yes, I agree with you Adrian but although I don't know anything about coding it seems that your code will fix only the index.html part. The other code is much longer and more complicated but it looks it will redirect all of the www .domain variations.

Don't you agree? Could you please tell me why don't you like it? Is there anything in that code that will affect my site in a bad way?
  • 0
PremiumMember
chatyak86
Posts: 1085
Joined: 17 Jun 06
Trust:
Hi Crowzy,

Yes you are right. That would only fix the index part. I don't know much about php coding myself although this coding should work for you. You could use what I gave you, skip a line, and then put this.

Create a .htaccess file with the below code, it will ensure that all requests coming in to domain.com will get redirected to [http://www.domain.com]
The .htaccess file needs to be placed in the root directory of your old website (i.e the same directory where your index file is placed)

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ [http://www.domain.com/$1] [r=301,nc]

Please REPLACE [domain.com] and [http://www.newdomain.com] with your actual domain name.

Note* This .htaccess method of redirection works ONLY on Linux servers having the Apache Mod-Rewrite moduled enabled.


This is a 301 redirect so it should not hurt your site... I honestly have no idea why it would.
  • 0
crowzy
Posts: 40
Joined: 15 Dec 06
Trust:
Hi again Adrian,

I've tried what you suggested but it didn't work. I couldn't open my site with any variation. Then I deleted the php code and left only the one that you gave me. That didn't make any difference either. Still couldn't open it. At the end I deleted everything and now it is working fine (like before), but there are no redirections in place.

Any ideas why?
  • 0
PremiumMember
chatyak86
Posts: 1085
Joined: 17 Jun 06
Trust:
You will have to email your web host tech support and ask them why. There could be some sort of hold on .htaccess files. I had issues with mine once.
  • 0
PremiumMember
eddie53
Posts: 37
Joined: 24 Aug 06
Trust:
Hi Crowzy,

This is what I use

# Begin non-www page protection #
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_Host} !^www\.marketingstrategiescenter\.com [NC]
RewriteRule ^(.*)$ [http://www.marketingstrategiescenter.com/$1] [L,R=301]
</IfModule>
# End non-www page protection #

Hope this helps
  • 0
crowzy
Posts: 40
Joined: 15 Dec 06
Trust:
Hi Eddie,

Thanks a lot. This one works perfectly. The only thing I can't redirect now is [http://www.domain.com/index.html].
  • 0