Pages: [1] 2   Go Down
Send this topic | Print
Author Topic: [Scripts] PHP Script & HTACCESS: Removing "www" (Or Adding) To Site [Piotr GRD]  (Read 5735 times)
d3xt3r
Guest
« on: June 04, 2008, 06:57:20 PM »

The talented Piotr GRD posted much more reliable, and versatile scripts that do even more. So, in light of this, I removed this post, as to not confuse others, and so others have better options and scripts to do similar or opposite on their websites. Smiley

VVV See Piotr GRD's Scripts Below VVV
« Last Edit: June 26, 2008, 06:56:57 PM by D3xt3r » Logged
Piotr GRD
Honoured 110MB Member
Official 110mb Guru
*****
Offline Offline

Posts: 6667



WWW
« Reply #1 on: June 04, 2008, 08:03:36 PM »

PHP USUALLY CAN easily detect the "www" part in the address by checking HTTP_HOST or SERVER_NAME. It will be "yourdomain.com" without www and "www.yourdomain.com" with www.
But on 110mb (because of handling www part with an internal proxy) HTTP_HOST never contains "www".

You can detect the IPs, but more easy is to use HTTP_VIA (or HTTP:VIA in .htaccess) - you can find it in many posts on this forum in technical section.

If you entering without "www" HTTP_VIA is empty (or have some value of other proxy if is in use by the client).
If you entering WITH "www" HTTP_VIA contains "1.1 www.mysite.110mb.com" or "1.1 www.mysite.911mb.com" or "1.1 www.myextradomain.com".



PHP
For making this code as easy as possible and fit to all addresses (.110mb.com .911mb.com .extradomain) you can put this at the very beginning of your all pages:

if you want to force non-www address:
www.yourextradomain.com   ->   yourextradomain.com
www.subdomain.110mb.com   ->   subdomain.110mb.com
www.subdomain.911mb.com   ->   subdomain.911mb.com

(you don't have to edit anything)
Code:
<?php
if (preg_match('/www\.'.$_SERVER['HTTP_HOST'].'/i', $_SERVER['HTTP_VIA']))
{
header("HTTP/1.1 301 Moved Permanently");
  
header("Location: http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
  exit();
}
?>

if you want to force with-www address:
yourextradomain.com   ->   www.yourextradomain.com
subdomain.110mb.com   ->   www.subdomain.110mb.com
subdomain.911mb.com   ->   www.subdomain.911mb.com

(you don't have to edit anything)
Code:
<?php
if (!preg_match('/www\.'.$_SERVER['HTTP_HOST'].'/i', $_SERVER['HTTP_VIA']))
{
header("HTTP/1.1 301 Moved Permanently");
  
header("Location: http://www." . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
  exit();
}
?>

if you want to force only one non-www address (redirect 110mb subdomain to own domain in example):
www.yourextradomain.com   ->   yourextradomain.com
www.subdomain.110mb.com   ->   yourextradomain.com
subdomain.110mb.com       ->   yourextradomain.com
www.subdomain.911mb.com   ->   yourextradomain.com
subdomain.911mb.com       ->   yourextradomain.com

(you have to edit your preferred domain - without www part in second line)
Code:
<?php
$preferreddomain
= 'ownextradomain.com';   // without www
if ($_SERVER['HTTP_HOST'] != $preferreddomain || preg_match('/www\.'.$preffereddomain.'/i', $_SERVER['HTTP_VIA']))
{
header("HTTP/1.1 301 Moved Permanently");
  
header("Location: http://" . $preferreddomain . $_SERVER['REQUEST_URI']);
  exit();
}
?>

if you want to force only one with-www address (redirect 110mb subdomain to own domain in example):
yourextradomain.com   ->   www.yourextradomain.com
subdomain.110mb.com   ->   www.yourextradomain.com
www.subdomain.110mb.com -> www.yourextradomain.com
subdomain.911mb.com   ->   www.yourextradomain.com
www.subdomain.911mb.com -> www.yourextradomain.com

(you have to edit your preferred domain - without www part in second line)
Code:
<?php
$preferreddomain
= 'ownextradomain.com';   // without www
if ($_SERVER['HTTP_HOST'] != $preferreddomain && !preg_match('/www\.'.$preffereddomain.'/i', $_SERVER['HTTP_VIA']))
{
header("HTTP/1.1 301 Moved Permanently");
  
header("Location: http://www." . $preferreddomain . $_SERVER['REQUEST_URI']);
  exit();
}
?>



.htaccess
For doing the same with .htaccess (if you buy it here) - create file called ".htaccess" and upload it into your root folder on your account (or add this code to your existing .htaccess file):
(If you have any doubts - ask some .htaccess expert how to integrate your code.)

if you want to force non-www address:
www.yourextradomain.com   ->   yourextradomain.com
www.subdomain.110mb.com   ->   subdomain.110mb.com
www.subdomain.911mb.com   ->   subdomain.911mb.com

(you have to edit names of your domain and 110mb subdomain - keep the syntax with backslashes!)
Code:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP:VIA} www\.yourdomain\.com$
RewriteRule ^(.*)$ http://yourdomain.com/$1 [R=301,L]
RewriteCond %{HTTP:VIA} www\.subdomain\.110mb\.com$
RewriteRule ^(.*)$ http://subdomain.110mb.com/$1 [R=301,L]
RewriteCond %{HTTP:VIA} www\.subdomain\.911mb\.com$
RewriteRule ^(.*)$ http://subdomain.911mb.com/$1 [R=301,L]

if you want to force with-www address:
yourextradomain.com   ->   www.yourextradomain.com
subdomain.110mb.com   ->   www.subdomain.110mb.com
subdomain.911mb.com   ->   www.subdomain.911mb.com

(you have to edit names of your domain and 110mb subdomain - keep the syntax with backslashes!)
Code:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP:VIA} !www\.yourdomain\.com$
RewriteCond %{HTTP_HOST} yourdomain\.com$
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=301,L]
RewriteCond %{HTTP:VIA} !www\.subdomain\.110mb\.com$
RewriteCond %{HTTP_HOST} subdomain\.110mb\.com$
RewriteRule ^(.*)$ http://www.subdomain.110mb.com/$1 [R=301,L]
RewriteCond %{HTTP:VIA} !www\.subdomain\.911mb\.com$
RewriteCond %{HTTP_HOST} subdomain\.911mb\.com$
RewriteRule ^(.*)$ http://www.subdomain.911mb.com/$1 [R=301,L]

if you want to force only one non-www address (redirect 110mb subdomain to own domain in example):
www.yourextradomain.com   ->   yourextradomain.com
www.subdomain.110mb.com   ->   yourextradomain.com
subdomain.110mb.com       ->   yourextradomain.com
www.subdomain.911mb.com   ->   yourextradomain.com
subdomain.911mb.com       ->   yourextradomain.com

(you have to edit name of your domain - keep the syntax with backslashes!)
Code:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP:VIA} www\.yourdomain\.com$ [OR]
RewriteCond %{HTTP_HOST} !yourdomain\.com$
RewriteRule ^(.*)$ http://yourdomain.com/$1 [R=301,L]

if you want to force only one with-www address (redirect 110mb subdomain to own domain in example):
yourextradomain.com   ->   www.yourextradomain.com
subdomain.110mb.com   ->   www.yourextradomain.com
www.subdomain.110mb.com -> www.yourextradomain.com
subdomain.911mb.com   ->   www.yourextradomain.com
www.subdomain.911mb.com -> www.yourextradomain.com

(you have to edit name of your domain - keep the syntax with backslashes!)
Code:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP:VIA} !www\.yourdomain\.com$ [OR]
RewriteCond %{HTTP_HOST} !yourdomain\.com$
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=301,L]



.htaccess simplified
If you care only about one domain (own or 110mb sub-) and don't care about other addresses (like 911mb subdomain).

if you want to force non-www address:
(you have to edit name of your domain or 110mb subdomain - keep the syntax with backslashes!)
Code:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP:VIA} www\.yourdomain\.com$
RewriteRule ^(.*)$ http://yourdomain.com/$1 [R=301,L]

if you want to force with-www address:
(you have to edit name of your domain or 110mb subdomain - keep the syntax with backslashes!)
Code:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP:VIA} !www\.yourdomain\.com$
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=301,L]



NOTE: above codes are specific especially for 110mb servers configuration.
These codes will not work correctly on other servers (your local in example) and vice versa - codes that works on other servers will not work correctly on 110mb servers with current configuration.





PS. Sorry, D3xt3r, that I am inputting my own three words, but I think using HTTP_VIA is more easy. With using IPs non-advanced users can ask - "what is my IP?". And sometimes (like box13 and box14) IP can change and handling www with proxy on 110mb probably will never change so HTTP_VIA will be the same.
« Last Edit: February 11, 2009, 12:59:11 AM by Piotr GRD » Logged

d3xt3r
Guest
« Reply #2 on: June 04, 2008, 08:35:15 PM »

No problem Piotr. I was only thinking of the system here, and I was unaware of the HTTP_VIA protocol. So your script is better.

And I was aware of the HTACCESS ability. But didn't post info regarding it. Wink
Logged
Gospel
Hyper-Active Member
***
Offline Offline

Posts: 167


Jesus gives eternal life


WWW
« Reply #3 on: June 13, 2008, 05:35:30 AM »


For doing the same with .htaccess (if you buy it here):

.htaccess: www -> non-www:
Code:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP:VIA} www\.yourdomain\.com$
RewriteRule ^(.*)$ http://yourdomain.com/$1 [R=301,L]

.htaccess: non-www -> www:
Code:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP:VIA} !www\.yourdomain\.com$
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=301,L]

Thanks at all, it works fine on my website.  smiley
Logged

TDSii
Best warez site!!
Loyal 110MB Member
*******
Offline Offline

Posts: 2056


..:: skdown.net ::..


WWW
« Reply #4 on: June 19, 2008, 09:30:07 AM »

should be added to the knowledge base in support!
Big time guys!
Logged


Piotr GRD
Honoured 110MB Member
Official 110mb Guru
*****
Offline Offline

Posts: 6667



WWW
« Reply #5 on: June 20, 2008, 02:03:59 AM »

My above post is just edited.
Full advanced .htaccess codes are added.
(not tested, but should work - with all my knowledge)
« Last Edit: June 20, 2008, 02:10:49 AM by Piotr GRD » Logged

bedrock
Member
*
Offline Offline

Posts: 13


« Reply #6 on: June 22, 2008, 10:01:39 PM »

Hi Piotr GRD,

I am trying to follow your guide to force www on my website, but i keep getting in a redirect loop Sad firefox reports the following:

Code:
Firefox has detected that the server is redirecting the request for this address in a way that will never complete.

Currently my .htaccess and index.php are as follows:

Code:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

Code:
<?php
if (!ereg("www", $_SERVER['HTTP_VIA']))
{
$location = 'http://www.nicbedford.co.uk' . $_SERVER['REQUEST_URI'];
  
header ("location: $location");
  exit(); }

/* Short and sweet */
define('WP_USE_THEMES', true);
require(
'./wordpress/wp-blog-header.php');
?>

Any suggestions on how to best achieve forcing www. (through either .htaccess or php) on the front, avoiding a redirect loop? The above has been working for several months (i'm sure it was) but my site is on box11 and after the recent problems with it i haven't been able to get thsi working, im not sure if it's me or some other problem.

Thanks in advance, Nic
Logged
Piotr GRD
Honoured 110MB Member
Official 110mb Guru
*****
Offline Offline

Posts: 6667



WWW
« Reply #7 on: June 22, 2008, 10:12:52 PM »

It was working, you didn't change anything and now it's not?

Apparently after last problems with high load on box11 all request with-www are now redirected to non-www (this is global setting for this box to avoid additional resource consuption for the "www proxy"). You are redirecting to with-www, global to non-www, you to with-www etc....

I think it's temporary until they will fix problems with box11.
But it's still not recommended to use www in front.
« Last Edit: June 22, 2008, 10:14:47 PM by Piotr GRD » Logged

bedrock
Member
*
Offline Offline

Posts: 13


« Reply #8 on: June 23, 2008, 12:00:53 AM »

Thanks for reply, so when box11 high load problems are stable again, my current codes will work for www redirection. This is good to know, for now i remove redirection from index.php and just have non-www url's
Logged
d3xt3r
Guest
« Reply #9 on: June 23, 2008, 12:43:15 AM »

Its not a certainty that it will work. But as far as everyone knows, when the box is stabilized the proxy system should be re-integrated, and www should work. Wink
Logged
Piotr GRD
Honoured 110MB Member
Official 110mb Guru
*****
Offline Offline

Posts: 6667



WWW
« Reply #10 on: June 23, 2008, 01:43:47 AM »

Also be aware that your redirection code (I recognise my old idea in it) is not redirecting for some specific connections while someone is using at least one other proxy server.

If you still want to redirect to www address please use one of the above new updated by me codes (it's more unfailing and faster).



I also just realised that there is missing "exit" in my PHP codes - updated! grin
Logged

bedrock
Member
*
Offline Offline

Posts: 13


« Reply #11 on: June 23, 2008, 04:30:08 AM »

Ok, thanks a lot guys.

Piotr GRD, I'm sorry if this is a dumb question, but i don't understand the difference between force with-www address and force only one with-www address and which one i should use to replace my not so good code

Thanks in advance
Logged
Piotr GRD
Honoured 110MB Member
Official 110mb Guru
*****
Offline Offline

Posts: 6667



WWW
« Reply #12 on: June 23, 2008, 05:11:52 AM »

Yes. Some peoples don't know or don't realise that the files can be accessed trough three addresses here on 110mb. So explanation to my above codes:


Force non-www address:
www.yourextradomain.com   ->   yourextradomain.com
www.subdomain.110mb.com   ->   subdomain.110mb.com
www.subdomain.911mb.com   ->   subdomain.911mb.com


Force with-www address:
yourextradomain.com   ->   www.yourextradomain.com
subdomain.110mb.com   ->   www.subdomain.110mb.com
subdomain.911mb.com   ->   www.subdomain.911mb.com


Force only one non-www address:
(own extra domain in example)
www.yourextradomain.com   ->   yourextradomain.com
www.subdomain.110mb.com   ->   yourextradomain.com
subdomain.110mb.com       ->   yourextradomain.com
www.subdomain.911mb.com   ->   yourextradomain.com
subdomain.911mb.com       ->   yourextradomain.com


Force only one with-www address:
(own extra domain in example)
yourextradomain.com   ->   www.yourextradomain.com
subdomain.110mb.com   ->   www.yourextradomain.com
www.subdomain.110mb.com -> www.yourextradomain.com
subdomain.911mb.com   ->   www.yourextradomain.com
www.subdomain.911mb.com -> www.yourextradomain.com

« Last Edit: June 23, 2008, 05:16:52 AM by Piotr GRD » Logged

Myles Grey
Full-Time Programmer
Super Authority member
******
Offline Offline

Posts: 1863


Programmer's Delight Release - 90%


WWW
« Reply #13 on: June 26, 2008, 10:24:01 AM »

Firstly, thanks for the scripts, they're great! But I have a problem. I'm using Force only one non-www address, but it's redirectly like this:

yourextradomain.com   ->   yourextradomain.com
subdomain.110mb.com   ->   www.yourextradomain.com
www.subdomain.110mb.com -> www.subdomain.110mb.com
Logged

IE has the best Quirks
Almost every site you see runs on Quirks, not standards




Remember to mark your topic as [resolved]
Piotr GRD
Honoured 110MB Member
Official 110mb Guru
*****
Offline Offline

Posts: 6667



WWW
« Reply #14 on: June 26, 2008, 06:10:00 PM »

I know that the admins are currently working on some patch to the Apache server that will allow to handle "www" as fast as the "non-www". So above codes can be outdated soon. As for now I am redirecting to only one non-www address with my above PHP code on box4 and it's working fine. You can check: www.freeball.110mb.com . Maybe on some other box they are playing with server configuration at the moment and that's why it's not working. But - of course - I can not be sure. As I wrote before: above codes are specific especially for 110mb servers configuration. Current configuration that I know. While it's changing then...


You can write the following code:
Code:
<?php
echo 'HTTP_HOST: '.$_SERVER['HTTP_HOST'].'<br/>';
echo
'HTTP_VIA: '.$_SERVER['HTTP_VIA'].'<br/>';
?>
Save it on your account and open this file in the browser with different addresses - with and without www, on your own and on 110mb subdomain and provide what values of HTTP_HOST and HTTP_VIA you have on which addresses. On those values are basing all my redirection codes.
You can check this here on box4: grd.110mb.com/_test_/ <- go with and wthout www and also on extra domain grd.go.pl/_test_/
« Last Edit: June 26, 2008, 06:15:26 PM by Piotr GRD » Logged

webbuilderpro
Member
*
Offline Offline

Posts: 21


WWW
« Reply #15 on: July 12, 2008, 07:21:24 PM »

thanks for information bro
Logged
CESAR
18 Years Old Webmaster
Hyper-Active Member
***
Offline Offline

Posts: 184


Member Since: 10-09-2006


WWW
« Reply #16 on: August 19, 2008, 07:24:14 PM »

Thanks Piotr, I was searching for this longtime ago! since all my website sub domain is published everywhere!  wink
Logged

[Box 4]http://pimps-clan.110mb.com This Site Worth:
[Box 14]http://thewowuniverse.com This Site Worth
Piotr GRD
Honoured 110MB Member
Official 110mb Guru
*****
Offline Offline

Posts: 6667



WWW
« Reply #17 on: August 27, 2008, 02:12:35 AM »

I just think that alternative and faster method of forcing non-www address for own domains should be posted here, too.

Why faster and why only for own extra domains? Because done already on the nameservers.

How? You have to:
- NOT use 110mb nameservers, but some external;
- at your DNS service delete "CNAME www -> @" and/or A record for "www";
- ! ! keep the A record for "@" (for your domain without www) ! ! ;
- create "www." subdomain for your domain and redirect it to yourdomain without www.
Note: probably not at all DNS services you can do that.


Idea: Dermah
His "how to" on instance of GoDaddy:


This is for GoDaddy domains:
Log into your account. Click on domains so that you go to their domain control panel.
If you haven't already, remove the www CNAME entry by clicking on Total DNS Control and MX Records and clicking the x at the end of the www line.
Go back to the GoDaddy domain control panel and in the bottom left corner there should be a box called 'Subdomains: x Available (add)' Click on add.
In the slide down wizard click on the Forwarding tab then check Enable Forwarding if it isn't already.
In the subdomain field enter 'www' (without the quotes) and in the Forward to field enter you domain WITHOUT THE WWW's
I don't know if you should pick 301 moved permanently or 302 moved temporarily. I picked permanently.
Click OK. Wait for 2-12 hours.

EDIT:

« Last Edit: August 27, 2008, 02:14:23 AM by Piotr GRD » Logged

downloadfreesongs
Member
*
Offline Offline

Posts: 5


WWW
« Reply #18 on: March 16, 2009, 08:19:54 PM »

you must learn it yourself
Logged
mybboard
PHP GURU
Active Member
**
Offline Offline

Posts: 54

www.mybboard.110mb.com


WWW
« Reply #19 on: April 16, 2009, 03:26:31 PM »

so is there a way to change a directory to the subdomain means,
I have a frum on this,
www.DOMAINNAME.com/forum

Can I change it to this

www.forum.domainname.com

If yes, how ??
Logged

www.mybboard.110mb.com <--- Exclusive Themes, Plugins, Tutorials and Support Website.
Pages: [1] 2   Go Up
Send this topic | Print
Jump to: