I noticed that Google somehow indexed my site but with incorrect subdomain. Instead of putting a link to www.mydomain.com or mydomain.com it shows ns1.mydomain.com and ns2.mydomain.com, which results in broken images, because I used relative paths.
Now, I don't even have ns1 and ns2 setup as subdomains. I have those in my DNS settings.
So, how do I go about redirecting users to the correct address?
Thanks
OK, I think I was able to fix this by placing this into my .htaccess file
RewriteCond %{HTTP_HOST} ^ns1.mydomain\.com
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=permanent,L]
But even though I fixed it, I am very curious how did Google index those links anyway?
Related
I have a wordpress hosting account. I also have a standard web hosting account with the same host. My main website, domain.com is hosted on the Wordpress platform, BUT now I want to add a subdomain for sub.domain.com.
I can't host the script on sub.domain.com as part of the wordpress hosting, so I have been told to create it on my standard web hosting account and then use an A record in the DNS for domain.com to point to the IP of sub.domain.com.
Now, all this works if I visit http://sub.domain.com. However, www.sub.domain.com doesn't work.
What I want to know is, can I edit the .htaccess for domain.com to redirect anyone who visits www.sub.domain.com to http://sub.domain.com?
You can use this:
RewriteCond %{HTTP_HOST} ^www\.sub\.domain\.com [NC]
RewriteRule ^(.*) http://sub.domain.com/$1 [L,R=301]
You can't use .htaccess to redirect traffic from www.sub.domain.com to sub.domain.com, because that traffic won't ever get to your server unless your DNS is set up correctly. In other words, if there's no DNS record for www.sub.domain.com, traffic looking for that address will never hit your server, so what is in your .htaccess would be irrelevant.
Keep in mind that you don't have to have www.sub.domain.com - almost nobody will visit that unless you share links using that domain yourself.
That said, if you really want to do this:
Create a sub-sub domain by creating an A record for www.sub.domain.com
Edit the .htaccess file for that sub-sub domain only, adding this rule:
RewriteRule ^(.*) http://sub.domain.com/$1 [L,R=301]
Plesk offers you a handy way to preview the site before you switch over the DNS, called Websites Preview.
This is what it does:
customer-site.tld will be available for preview as customer-site.tld.192-0-2-12.your-domain.tld. Here 192-0-2-12 is the site's IP where dots are replaced with dashes
I find this feature very useful, as we use it for proofing websites before they go live, however after the site goes live the customer-site.tld.192-0-2-12.your-domain.tld URL still exists, and is showing up in some Google results!
How can I remove the preview URL for certain sites only, not for all sites on the server?
Alternatively I think I may be able to use a .htaccess rule for this, but I cannot figure out how to do that without a redirect loop.
I found the answer to this with some more searching (I searched for how to redirect domain alias, as that's what this essentially is).
The answer is here: Redirecting all domain aliases to one with htaccess
RewriteEngine On
# If the hostname is NOT www.domain.com
RewriteCond %{HTTP_HOST} !^www\.domain\.com$
# 301 redirect to the same resource on www.domain.com
RewriteRule (.*) http://www.domain.com/$1 [L,R=301]
Use robots.txt file to prevent crawlers from indexing your page. After you have done DNS switching, remove robots.txt file
So I have been searching the net along with this site regarding htaccess redirections, but have yet not managed to find the solution for myself (it may not even be possible).
So my scenario is this:
I have bought a cheap host, which comes with a subdomain, but also has the ability to add your own domain as an alias to it. Right now I can access the site using:
subdomain.hostprovider.com
and
mydomainasalias.com
What I wish to achieve here, is that I would be able to redirect a subdomain from the domain alias to a random page on a random site.
So
subdomain.mydomainasalias.com
is basically
2ndsubdomain.subdomain.hostprovider.com
I have tried this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain\.mydomainasalias\.com$ [NC]
RewriteRule ^ http://randomsite.com/asd [L,R]
amongst some other things, to achieve this, but have yet not succeeded. Is it even possible?
I need to redirect some users, possibly to a subdomain, but I do not want them to know that they are in a subdomain. They should think they are in the main domain.
I believe this can be done with htaccess, but it is gibberish to me.
Can someone please throw me some bones?
When serving content from a different domain without redirecting the browser (thus changing the URL in the address bar) one of two things needs to happen. Either there is a file-path resolution to the other domain, or a reverse proxy must be set up (and it's pretty easy to do if mod_proxy is loaded).
It looks like you have your subdomain inside the document root of your main domain, which means this option will be viable. So if you want to it so when someone puts this URL in their address bar, http://domain.com/page, they get served the content in http://sub.domain.com/another-page, you'd simply add these rules to the top of the htaccess file in the document root of your main domain (public_html):
RewriteEngine On
RewriteRule ^page(.*)$ /subdomain_folder/another-page$1 [L]
Otherwise, the second option is to use mod_proxy:
RewriteEngine On
RewriteRule ^page(.*)$ http://sub.domain.com/another-page$1 [L,P]
EDIT:
I want to redirect some users, depending on their ip (geographic location) to another subdomain. And yes public_html is the root
You can check against the IP via the %{REMOTE_ADDR} variable:
RewriteCond %{REMOTE_ADDR} ^123\.45\.67\.89$
RewriteCond !^/subdomain_folder
RewriteRule ^(.*)$ /subdomain_folder/$1 [L]
My domain http://bretthowardsproul.com is active but the www version, www.bretthowardsproul.com , shows a blank screen.
I added this .htaccess but it doesn't change the results:
# Redirect www to no-www - via bit.ly/www-no-www
RewriteEngine On
RewriteCond %{HTTP_HOST} !^bretthowardsproul.com$ [NC]
RewriteRule ^(.*)$ http://bretthowardsproul.com/$1 [L,R=301]
I then tried test.bretthowardsproul.com and got a 404. So is it something in my .htaccess?
If not, do I need to do something with a CNAME? Is there something I can check in Firebug to see what's going on at www ?
Make sure the "www" CNAME exists for your domain, and is pointing to your # record, otherwise "www" traffic won't go to the same place that your domain goes to without "www".
A quick google search should help you figure out how to add the CNAME to your registrar. For GoDaddy, this is helpful: http://www.google.com/support/a/bin/answer.py?answer=47610