Jump to content
  • 0

Redirect 'www' pages to 'non www' pages .htaccess


Kyo

Question


  • Group:  Members
  • Topic Count:  6
  • Topics Per Day:  0.00
  • Content Count:  90
  • Reputation:   12
  • Joined:  08/22/12
  • Last Seen:  

Hey everyone,

 

I hope anyone ehre can help me out with a little problem I have. I need a .htaccess code that redirects www pages to non www pages. Here's what I have:

 

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

 

The code works, my only problem about it is that www.subdomain.domain.com doesn't work. www.domain.com/subdomain works just fine. I really need www.subdomain.domain.com working though, I hope anyone here could help me out here.

 

Thanks in advance,

Kyo

Edited by Kyo
Link to comment
Share on other sites

7 answers to this question

Recommended Posts


  • Group:  Members
  • Topic Count:  0
  • Topics Per Day:  0
  • Content Count:  6
  • Reputation:   7
  • Joined:  01/09/12
  • Last Seen:  

If there is just one subdomain or a limited amount you can do it one by one with:

 

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www.subdomain.domain.com$ [NC]
RewriteRule ^(.*)$ http://subdomain.domain.com/$1 [R=301]
 
Otherwise it gets more complicated with the rewrite rules and I won't get into that.
Link to comment
Share on other sites


  • Group:  Members
  • Topic Count:  20
  • Topics Per Day:  0.00
  • Content Count:  118
  • Reputation:   6
  • Joined:  12/20/12
  • Last Seen:  

try this

 

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

 

And make sure that your hp user have execute permission on the .htaccess

 

just for test propouse use chmod 777 if it works make sure that use 700 chmod or 400 (i think this was only execute) and the propper user

 

:)

Link to comment
Share on other sites


  • Group:  Members
  • Topic Count:  6
  • Topics Per Day:  0.00
  • Content Count:  90
  • Reputation:   12
  • Joined:  08/22/12
  • Last Seen:  

Sorry, I guess you missunderstood me. I need 'www' pages to 'non www' and not the other way around. However, same result. www.subdomain.domain.com doesn't work - the www doesn't get removed, www.domain.com/subdomain gets the www removed.

 

Thanks for the help though.

Link to comment
Share on other sites


  • Group:  Members
  • Topic Count:  20
  • Topics Per Day:  0.00
  • Content Count:  118
  • Reputation:   6
  • Joined:  12/20/12
  • Last Seen:  

I dont know if i get what you need, 

 

i found this..

 

 


There is a project that requires me to make the following domain redirection:
  • Redirect all users to access the site WITHOUT the 'www.' prefix for all subdomain.
  • Redirect all users to access the site WITH the 'www.' prefix for main domain only.
To fulfill this purpose, I simply add a few lines of code to the htaccess file:

RewriteEngine on

# Redirect the 'www.' version of the subdomains.
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.example\.com [NC]
RewriteRule ^(.*)$ http://%1.example.com/$1 [R=301,L]

# Redirect the non 'www.' version of the main domain.
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

 

is this what you need? o.o? or i'm still missing some out =(?

Link to comment
Share on other sites


  • Group:  Members
  • Topic Count:  6
  • Topics Per Day:  0.00
  • Content Count:  90
  • Reputation:   12
  • Joined:  08/22/12
  • Last Seen:  

If there is just one subdomain or a limited amount you can do it one by one with:

 

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www.subdomain.domain.com$ [NC]
RewriteRule ^(.*)$ http://subdomain.domain.com/$1 [R=301]
 
Otherwise it gets more complicated with the rewrite rules and I won't get into that.

I tried that already, it didn't work for me. Here's the code:

 

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

 

I dont know if i get what you need, 

 

i found this..

 

 

There is a project that requires me to make the following domain redirection:
  • Redirect all users to access the site WITHOUT the 'www.' prefix for all subdomain.
  • Redirect all users to access the site WITH the 'www.' prefix for main domain only.
To fulfill this purpose, I simply add a few lines of code to the htaccess file:

RewriteEngine on

# Redirect the 'www.' version of the subdomains.

RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.example\.com [NC]

RewriteRule ^(.*)$ http://%1.example.com/$1 [R=301,L]

# Redirect the non 'www.' version of the main domain.

RewriteCond %{HTTP_HOST} ^example\.com$ [NC]

RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

 

is this what you need? o.o? or i'm still missing some out =(?

Yes, this is what I mean. For some reason it doesn't work for subdomains though. Just same with the code I had, just for the main domain.

 

PS: I always clear the cache and cookies before testing.

 

Edit: Wtf is wrong with the code box, it always removes all the text after it if there's any and I have to rewrite everything...

Edited by Kyo
Link to comment
Share on other sites


  • Group:  Members
  • Topic Count:  0
  • Topics Per Day:  0
  • Content Count:  6
  • Reputation:   7
  • Joined:  01/09/12
  • Last Seen:  

Actually, where are you putting the .htaccess ?  You should put it at the doc root of the subdomain.  I don't see you can access the main domain and subdomain at the same time.  If it's just one subdomain you are trying with.  I know .htaccess apply to all the dir under it, depend how you have your domain/subdomain dir structured.

Edited by yyCC
Link to comment
Share on other sites


  • Group:  Members
  • Topic Count:  6
  • Topics Per Day:  0.00
  • Content Count:  90
  • Reputation:   12
  • Joined:  08/22/12
  • Last Seen:  

Thanks yC and Nana, I got it working now. yC's code suggestion worked in the end. It just didn't work because I didn't make the www.subdomain subdomains, just made DNS entries for them.

 

This can get closed now.

Link to comment
Share on other sites

×
×
  • Create New...