Remove Plesk preview domain redirect - .htaccess

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

Related

Google search for my site is missing "www" prefix

I have a problem similar to this, but changing the .htaccess file hasn't solved my problem.
I have addressed this issue by including the following two rules into my .htaccess file. It now correctly redirects any http requests without the www prefix, to the page with it. The problem now is that google is still displaying the site without the prefix. The homepage is showing on google with the www. prefix, but not other pages.
RewriteCond %{HTTP_HOST} ^websitename.com [NC]
RewriteRule ^(.*)$ http://www.websitename.com/$1 [L,R=301]
Use Google Search Console:
Specify a preferred domain: On the Search Console Home page, click the
site you want. Click the gear icon , and then click Site Settings. In
the Preferred domain section, select the option you want.
Set your preferred domain (www or non-www)

How to give a subpage it's own domain name?

I have a Joomla site mysite.com.
I want to have the subpage mysite.com/example act like a separate website using a separate domain name anothersite.com.
Can this be achieved using DNS settings on the domain and htaccess?
Hosting sites at cloudaccess.net but want this solution for one-page sites and landing pages without using a whole other hosting account / joomla instance.
Thanks.
If your hosting provider allows you to have "alias" domains, you can point the new domain at the existing code, and detect the hostname using RewriteCond:
Start by configuring the new domain to display all the same content as the existing domain.
In the Apache configuration (probably .htaccess on shared hosting) add a rule which detects requests for the new domain, and "rewrites" them to instead serve your specific page:
RewriteCond %{HTTP_HOST} landingpage.example.com
RewriteRule ^/$ /url/for/my/landing/page
Add an additional rule afterwards so that other URLs on that domain redirect the browser back to the main site
RewriteCond %{HTTP_HOST} landingpage.example.com
RewriteRule ^/([^/].*)$ http://realsite.example.com/$1 [R=permanent,L]
A variation of this would be to map everything on the second domain to a directory (or, rather, a URL prefix) in the first domain, by combining those two rules:
RewriteCond %{HTTP_HOST} subsite.example.com
RewriteRule ^/(.*)$ /url/for/my/subsite/$1 [L]
Note: If Joomla thinks it knows the "correct" URL for the page, it may try to redirect away from your new domain. If so, that is the topic for a new question.
If, on the other hand, you want to use Apache configuration to force users accessing http://realsite.example.com/url/for/my/landing/page to be redirected to http://landingpage.example.com/, you could add something like this:
RewriteRule ^/url/for/my/landing/page$ http://landingpage.example.com/ [L,R=permanent]
Note: Depending on some environment settings I don't fully understand, you may need to change ^/ to just ^ in the patterns above.

.htaccess and A record subdomains

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]

Redirecting subdomain from an alias domain

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?

Want to redirect domain to blog in subfolder until full site launches

I am in the process of launching a full website that will be ready in about 6-9 months but I have a self-hosted Wordpress blog created in the meantime to start building a userbase and SEO. So since there will eventually be more to my site than a blog, my blog is located in a subfolder in my root domain (www.website.com/blog). I would like for my root domain (www.website.com) to automatically send users to my blog (www.website.com/blog). Once the full site is ready, I then want my users to enter my domain and be sent to the regular site and not the blog.
Through my searching, it seems like I will either have to set up a 301 or 302 redirect, which I understand how to do. The question is which one to use to prevent negative SEO once the full site launches. I have a feeling I should use a 302 redirect since it is a temporary redirect, but from what I have been reading, it seems like this may hurt my SEO on the popular search engines.
Your help would be greatly appreciated and I can provide more details if needed. Thanks!
I would suggest not to use external redirect and handle this via internal redirect only. That way your URL never changes and search engines will not index wrong URL. In your .htaccess put these rules for this purpose:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/+blog [NC]
RewriteRule ^(.*)$ blog/$1 [L,NE]
RewriteRule ^(/)?$ /blog [R=302]
That should do the trick.

Resources