1,105,328 Community Members

Redirect from all traffic to domain (.htaccess)

Member Avatar
Borzoi
Posting Whiz
344 posts since Oct 2006
Reputation Points: 24 [?]
Q&As Helped to Solve: 33 [?]
Skill Endorsements: 0 [?]
 
0
 

I have a server which is hosting just a single site and because of this, I intend to redirect all traffic from domains which isn't mine to my domain. For example, if a random person decides for some reason to point their domain to my server, when my server gets the request it redirects to my domain. I have tried various rules in teh .htaccess then going to the IP address to test this but each time I try, it stays on the IP address and doesn't redirect to the domain. Because of this, I decided to start working on just getting it to redirect from the IP to the domain. This is what my .htaccess file currently looks like:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^1\.2\.3\.4
RewriteRule (.*) http://domain.com/$1 [R=301,L] 

I have checked through my httpd.conf file and ensured that .htaccess is enabled and that the FollowSymLinks option is enabled (which is why it is not in the .htaccess file) but the redirect still isn't working. I've had a look online and I can't see anything wrong with what I have in my .htaccess file. Could someone give me some pointers on what I need to do or change?

You've probably already worked it out but the IP and domain in the above is not my IP or domain...

Member Avatar
GliderPilot
Posting Whiz in Training
296 posts since Sep 2006
Reputation Points: 31 [?]
Q&As Helped to Solve: 48 [?]
Skill Endorsements: 14 [?]
 
0
 

This should work for re-routing the domain, just change domain to your wanted domain:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.domain\.com$
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
Member Avatar
Borzoi
Posting Whiz
344 posts since Oct 2006
Reputation Points: 24 [?]
Q&As Helped to Solve: 33 [?]
Skill Endorsements: 0 [?]
 
0
 

That's what I had beforehand but that's not working. I want my site redirecting away from www and not to it so this is what I have for domain redirection:

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

I must have screwed something up in my httpd.conf file I reckon so I'm going to try rebuilding it and see if that helps.

Member Avatar
diafol
Where are my eyes?
12,977 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,848 [?]
Skill Endorsements: 92 [?]
Moderator
Featured
Sponsor
 
0
 
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,NC]

I think you're getting a lot of variations on the same theme. :(

Member Avatar
Borzoi
Posting Whiz
344 posts since Oct 2006
Reputation Points: 24 [?]
Q&As Helped to Solve: 33 [?]
Skill Endorsements: 0 [?]
 
2
 

I found the problem. It looks like I had accidentally changed the line specify the name of the .htaccess file in the httpd.conf file. The line should read:

AccessFileName .htaccess

But I had accidentally changed it to:

AccessFileName .htacess

Which was causing the problem. I've corrected it and restarted apache and it's now working correctly.

Question Answered as of 1 Year Ago by GliderPilot and diafol
Member Avatar
GliderPilot
Posting Whiz in Training
296 posts since Sep 2006
Reputation Points: 31 [?]
Q&As Helped to Solve: 48 [?]
Skill Endorsements: 14 [?]
 
0
 

Gotta love those 1 letter typos that just screw everything up! lol

Member Avatar
diafol
Where are my eyes?
12,977 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,848 [?]
Skill Endorsements: 92 [?]
Moderator
Featured
Sponsor
 
0
 

Glad you admitted to that. Well done! I was beginning to doubt the pooled widsom of the contributors :) +1 from me.

Member Avatar
Borzoi
Posting Whiz
344 posts since Oct 2006
Reputation Points: 24 [?]
Q&As Helped to Solve: 33 [?]
Skill Endorsements: 0 [?]
 
2
 

Pretending it started working randomly isn't going to help anyone. We're all human and miss things sometimes. Admitting to my mistake will hopefully help others who are having a similar problem.

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: