Forum Moderators: phranque

Message Too Old, No Replies

need to make subdomain go away

         

mikiowoko

2:51 pm on Apr 11, 2011 (gmt 0)

10+ Year Member



Somehow the search engines have indexed the UNhelpful subdomain created by cpanel on a shared server.
What I need to to is redirect anything asking for:

http://mysite.accountdomain.com/*


to

http://mysite.com/* 




Any help would be appreciated.

g1smd

2:57 pm on Apr 11, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Do they both resolve to the same physical folder inside the server, or a different folder?

mbabuskov

3:39 pm on Apr 11, 2011 (gmt 0)

10+ Year Member



You can play with .htaccess or, if you have a common .php file that gets loaded for each request (many frameworks work like that), you can use a simple PHP redirect at the start of index.php file:

if ($_SERVER["HTTP_HOST"] == 'mysite.accountdomain.com')
{
header('Location: http://mysite.com'.$_SERVER["REQUEST_URI"]);
exit();
}

mikiowoko

3:41 pm on Apr 11, 2011 (gmt 0)

10+ Year Member



The hosting account (hostgator shared)

is set up with everything in

/home/public_html/

for the account accountdomain


/home/public_html/mysite.com/

is where the site I speak of resides, mysite.com resolves to that subdirectory.

I want people to go to [mysite.com...]
I don't want people to be able to go to [mysite.accountdomain.com...]

what I tried that didn't work was adding the last two lines as below to the htaccess file and that didn't work :

to the htaccess file
below:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress
RewriteCond [mysite\.accountdomain(*)$...] [NC]
RewriteRule [mysite.$1...] [R=301,L]

mikiowoko

4:16 pm on Apr 11, 2011 (gmt 0)

10+ Year Member



@mbabuskov I didn't think I should mess with the index.php in a wordpress install, but it seems to work perfectly?

I added 1 line
header("HTTP/1.1 301");

would have preferred to do it with htaccess, but I have other stuff on my plate.

Thank you very much.

g1smd

5:39 pm on Apr 11, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Code that has been posted in one form or another several times every week for the last decade...

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


Any external redirect code such as this MUST be placed BEFORE all internal rewrite code otherwise the redirect exposes previously internally rewritten paths back out on to the web as new URLs.

Above "example.com" (one place) in pattern matches the physical server internal folder name where the site resides and "www.example.com" (two places) matches the domain name you want people to see and use.

mbabuskov

5:48 pm on Apr 11, 2011 (gmt 0)

10+ Year Member



I use it for CodeIgniter powered websites mostly where everything goes through index.php.

If you are hacking WP, make sure you also change files in wp-admin area, because those are accessed directly (at least in MU install, I don't have any standalone WP installation, so I can't check).

mikiowoko

6:18 pm on Apr 11, 2011 (gmt 0)

10+ Year Member



@g1smd

I am not using www at all, and in fact am using a wordpress plugin that forces a request with www to redirect to [domain.com...]

I have seen the code you are suggesting. That would not do what I need.

g1smd

7:02 pm on Apr 11, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The code was generalised.

Replace www.example.com with example.com in both places.