hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Hosting Security and Technology : Correct 301 moved permanently code
Reply

Hosting Security and Technology Configuring and optimizing web hosting servers and operating systems, developing administration scripts, building servers, protecting against hackers, and general security (SSL certificates, etc.)
Forum Jump

Correct 301 moved permanently code

Reply Post New Thread In Hosting Security and Technology Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 05-06-2009, 12:05 PM
NameSniper NameSniper is offline
Web Hosting Master
 
Join Date: Mar 2004
Posts: 995

Correct 301 moved permanently code


Hello,

I am trying to push my server to return Moved Permanently code and redirect these URLs to the main URL, but cant find the right code.
domain.com
w.domain.com
ww.domain.com
wwww.domain.com
to
www.domain.com

There are a number of codes available on the net and all supposed to do that, but which one is the eight one?

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

2)
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com
RewriteCond %{HTTP_HOST} ^w.example\.com
RewriteCond %{HTTP_HOST} ^ww.example\.com
RewriteCond %{HTTP_HOST} ^wwww.example\.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=permanent,L]

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

__________________
Best Regards,
Namesniper

Reply With Quote


Sponsored Links
  #2  
Old 05-06-2009, 12:08 PM
zoticaic zoticaic is offline
Premium Member
 
Join Date: Jun 2007
Location: Manila
Posts: 150
Check out the first set of rules from here: http://www.jhive.net/index.php/apach...-force-trailin

Reply With Quote
  #3  
Old 05-06-2009, 03:43 PM
NameSniper NameSniper is offline
Web Hosting Master
 
Join Date: Mar 2004
Posts: 995
Quote:
Originally Posted by zoticaic View Post
Check out the first set of rules from here: http://www.jhive.net/index.php/apach...-force-trailin
This one?

Code:
Options +FollowSymLinks
RewriteEngine On

RewriteBase /

# These first set of rules makes sure that visitors
# are viewing the WWW domain i.e. www.jhive.net
Rewritecond %{HTTP_HOST} !^www\.jhive\.net
RewriteRule (.*) http://www.jhive.net/$1/ [R=301,L]
It doesnt seems to be what I am looking for.
Anything else?

__________________
Best Regards,
Namesniper

Reply With Quote
Sponsored Links
  #4  
Old 05-06-2009, 03:51 PM
zoticaic zoticaic is offline
Premium Member
 
Join Date: Jun 2007
Location: Manila
Posts: 150
What exactly are you trying to achieve? i.e. if the domain is not like www.domain.com then redirect to that domain via 301?

I think that's exactly the solution, just have to change jhive.net to your correct domain.

Reply With Quote
  #5  
Old 05-06-2009, 06:17 PM
NameSniper NameSniper is offline
Web Hosting Master
 
Join Date: Mar 2004
Posts: 995
Quote:
Originally Posted by zoticaic View Post
What exactly are you trying to achieve? i.e. if the domain is not like www.domain.com then redirect to that domain via 301?

I think that's exactly the solution, just have to change jhive.net to your correct domain.
No, I want domain.com/* w.domain.com/* ww.domain.com/* wwww.domain.com/* to return moved permanently and redirect to the same page but at www.domain.com
For example is someone visits domain.com/somepage he should be taken to www.domain.com/somepage. Same about w.domain.com ww and wwww.
Is it possible?

__________________
Best Regards,
Namesniper

Reply With Quote
  #6  
Old 05-06-2009, 07:21 PM
foobic foobic is offline
Community Liaison 2.0
 
Join Date: Feb 2005
Location: Australia
Posts: 5,112
That's exactly what the code above does (the $1 substitutes your "somepage"). Most likely w. ww. and wwww.domain.com won't resolve though - you'd also need to add those as parked domains if you want them handled properly.

__________________
Chris

"Learn from the mistakes of others. You can never live long enough to make them all yourself." - Groucho Marx

Reply With Quote
  #7  
Old 05-06-2009, 08:42 PM
Lightwave Lightwave is offline
Web Hosting Master
 
Join Date: Apr 2003
Location: San Jose, CA.
Posts: 1,616
You fergot to say OR in your rules.

You're asking it to match a host which is domain.com, w.domain.com, ww.domain.com, AND wwww.domain.com (which it could only be one of)

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

__________________
Daved @ Lightwave Networking, LLC.
AS1426 https:/www.lightwave.net
Primary Bandwidth: EGIHosting (NLayer, Hurricane Electric, Global Crossing, Bandcon)
Xen PV VPS Hosting

Reply With Quote
  #8  
Old 05-06-2009, 09:00 PM
foobic foobic is offline
Community Liaison 2.0
 
Join Date: Feb 2005
Location: Australia
Posts: 5,112
Code:
Rewritecond %{HTTP_HOST} !^www\.jhive\.net
The one condition specifies any domain that is not www.domain.com, so it automatically handles all of those without needing the extra lines and multiple [OR]s.

__________________
Chris

"Learn from the mistakes of others. You can never live long enough to make them all yourself." - Groucho Marx

Reply With Quote
  #9  
Old 05-09-2009, 07:28 PM
NameSniper NameSniper is offline
Web Hosting Master
 
Join Date: Mar 2004
Posts: 995
Quote:
Originally Posted by Lightwave View Post
You fergot to say OR in your rules.

You're asking it to match a host which is domain.com, w.domain.com, ww.domain.com, AND wwww.domain.com (which it could only be one of)

Code:
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^domain.com [OR]
rewritecond %{http_host} ^w.domain.com [OR]
rewritecond %{http_host} ^ww.domain.com [OR]
rewritecond %{http_host} ^wwww.domain.com
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301]
I'll try this out, thanks.

Quote:
Originally Posted by foobic View Post
Code:
Rewritecond %{HTTP_HOST} !^www\.jhive\.net
The one condition specifies any domain that is not www.domain.com, so it automatically handles all of those without needing the extra lines and multiple [OR]s.
That code will return 301 for any subdomain except www.domain.com right?
Thats not what I need as I have a number of working subdomains and dont want to affect them.

__________________
Best Regards,
Namesniper

Reply With Quote
  #10  
Old 05-09-2009, 08:38 PM
zoticaic zoticaic is offline
Premium Member
 
Join Date: Jun 2007
Location: Manila
Posts: 150
Wink

Quote:
Originally Posted by NameSniper View Post
I'll try this out, thanks.


That code will return 301 for any subdomain except www.domain.com right?
Thats not what I need as I have a number of working subdomains and dont want to affect them.
This will not affect other subdomains the rewrite rules are effective only on the current ServerName/ServerAlias

For example if you have:

Code:
ServerName domain.com
ServerAlias www.domain.com
Apache will only redirect request to the corresponding virtual host block if the the specified domain is any of the above, and then the rewriting engine kicks in.

Reply With Quote
  #11  
Old 05-10-2009, 02:51 AM
foobic foobic is offline
Community Liaison 2.0
 
Join Date: Feb 2005
Location: Australia
Posts: 5,112
No, namesniper's right - where the subdomain has its document root in a subdirectory of the main domain's (the default setup for several control panels) the .htaccess in the parent directory will affect the subdomain. It doesn't feel right to me that this should happen but it does, and it's documented.

Where you have a number of subdomains that you want to match and a number of others you want to leave unaffected you have a choice of either using lightwave's suggestion (specify exactly what you want to match) or modifying zoticaic's to specify all those you don't want to match, ie.
Code:
Rewritecond %{HTTP_HOST} !^www\.example\.com
Rewritecond %{HTTP_HOST} !^sub1\.example\.com
Rewritecond %{HTTP_HOST} !^sub2\.example\.com
RewriteRule (.*) http://www.example.com/$1/ [R=301,L]

__________________
Chris

"Learn from the mistakes of others. You can never live long enough to make them all yourself." - Groucho Marx

Reply With Quote
  #12  
Old 05-12-2009, 05:10 PM
NameSniper NameSniper is offline
Web Hosting Master
 
Join Date: Mar 2004
Posts: 995
This code has worked out

Code:
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^domain.com [OR]
rewritecond %{http_host} ^w.domain.com [OR]
rewritecond %{http_host} ^ww.domain.com [OR]
rewritecond %{http_host} ^wwww.domain.com
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301]
I had a problem with w ww wwww domains, but this happens to be my control panel causing an issue.
In DirectAdmin you should add domain pointers like w.domain.com ww.domain.com and wwww.domain.com AND add them as an alias.

Quote:
Originally Posted by foobic View Post
No, namesniper's right - where the subdomain has its document root in a subdirectory of the main domain's (the default setup for several control panels) the .htaccess in the parent directory will affect the subdomain. It doesn't feel right to me that this should happen but it does, and it's documented.

Where you have a number of subdomains that you want to match and a number of others you want to leave unaffected you have a choice of either using lightwave's suggestion (specify exactly what you want to match) or modifying zoticaic's to specify all those you don't want to match, ie.
Code:
Rewritecond %{HTTP_HOST} !^www\.example\.com
Rewritecond %{HTTP_HOST} !^sub1\.example\.com
Rewritecond %{HTTP_HOST} !^sub2\.example\.com
RewriteRule (.*) http://www.example.com/$1/ [R=301,L]
Above code will exclude sub1 and sub2 from the list of moved sites?

__________________
Best Regards,
Namesniper

Reply With Quote
  #13  
Old 05-14-2009, 07:36 PM
NameSniper NameSniper is offline
Web Hosting Master
 
Join Date: Mar 2004
Posts: 995
I was also in the need of redirecting whole website except subdomain help.domain.com and this URL domain.com/admin.

Foobic suggested to use REQUEST_URI instead HTTP_HOST variable in .htaccess and it has worked out.

The code is
Code:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/admin
Rewritecond %{HTTP_HOST} !^help\.domain\.com
RewriteRule (.*) http://www.domain2.com/$1 [R=301,L]

__________________
Best Regards,
Namesniper

Reply With Quote
  #14  
Old 05-14-2009, 08:00 PM
foobic foobic is offline
Community Liaison 2.0
 
Join Date: Feb 2005
Location: Australia
Posts: 5,112
Thanks for sharing.

__________________
Chris

"Learn from the mistakes of others. You can never live long enough to make them all yourself." - Groucho Marx

Reply With Quote
  #15  
Old 05-14-2009, 08:20 PM
NameSniper NameSniper is offline
Web Hosting Master
 
Join Date: Mar 2004
Posts: 995
Just my contribution to community.

__________________
Best Regards,
Namesniper

Reply With Quote
Reply

Related posts from TheWhir.com
Title Type Date Posted
Rackspace Launches Fanatical Support for Developers Web Hosting News 2013-05-16 13:02:32
DigiCert Launches Extended Validation Code Signing Certificates Web Hosting News 2012-08-15 15:30:28
Web Hosts, Internet Industry Prepare for World IPv6 Launch Web Hosting News 2012-05-23 17:16:15
Web Host PEER 1 Migrates 1,600 Servers to New UK Data Center Web Hosting News 2012-02-17 14:16:22
Non-Profit New Zealand Computer Society to Develop Cloud Code of Practice Web Hosting News 2011-09-02 17:22:36


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Postbit Selector

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement:
Web Hosting News:



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?