How to point domain to url on external site using nameservers - .htaccess

Here is what I want to accomplish.
I have domain A which I want to update the nameservers to use the custom nameservers setup on domain B (i.e. ns1.domainb.com and ns2.domainb.com).
Then, when users go to domainA.com I want it to take them to domainB.com/domainA
How can I accomplish this? Can't I accomplish this via HTACCESS files on domainB?

When users go to domainA.com I want it to take them to
domainB.com/domainA
This solution explains how to achieve:
example.com --> domainB.com/example.com
DNS records and .htaccess
First, set the DNS of domainA.com to point to the IP address of domainB.com's server. In the root of domainB.com, create an .htaccess file with a rule to redirect/rewrite domainA.com requests to domainB.com/domainA. There are two methods and outcomes.
Method 1 - Silently rewrite the URI request
User navigates to http://example.com, browser shows http://example.com.
Set these .htaccess rules in the root of domainB.com:
RewriteEngine On
RewriteBase /
# Check that the request is NOT from domainB.com, e.g. domainA
RewriteCond %{HTTP_HOST} !domainB\.com$ [NC]
# Capture the top-level domainA name
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+?)$
# Check that we are not already requesting a valid file or directory
# This prevents inserting the subdirectory name into an already valid path
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite requests with the domainA subdirectory
RewriteRule (.*) /%1/$1 [L]
Method 2 - Explicitly force a redirect to the new location
User navigates to http://example.com, browser shows http://domainB.com/example.com.
Set these .htaccess rules in the root of domainB.com:
RewriteEngine On
RewriteBase /
# Check that the request is NOT from domainB.com, e.g. domainA
RewriteCond %{HTTP_HOST} !domainB\.com$ [NC]
# Capture the top-level domainA name
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+?)$
# Force a 301 redirect to the new URI
RewriteRule ^(.*)$ http://domainB.com/%1%{REQUEST_URI} [R=301,L]
Customization
At the time of this writing, you can test and modify (most) .htaccess rules to support your customized requirements here: http://htaccess.madewithlove.be/
Here are tips and tricks to achieve further customization: http://mod-rewrite-cheatsheet.com/

Related

How to redirect roodomain/addondomain urls to addondomain

My host does not know how to fix this.
I saw in google results URLs that worry me.
For example, I saw rootdomain/addondomain.com/url1.html etc
this happened because google bot was not redirected to addondomain.com/url1.html for example
So I want to redirect all URLs to addondomain.com only
Because this created duplicate content.
My root domain has nothing to do with addon domain...they have a completely different topic....
I already have redirection from addondomain.rootdomain.com to addon domain in htaccess....
but I want to add the new one too...
This is the code I already have
RewriteCond %{HTTP_HOST} ^addon\.root\.org$ [OR]
RewriteCond %{HTTP_HOST} ^www\.addon\.root\.org$
RewriteRule ^(.*)$ "https\:\/\/www\.addon\.com\/$1" [R=301,L]
here is the example with some random domains...
root domain is : bonesroot.com
addon domain is : beeraddon.com
and beerroot.com files are in the folder bones.com/beer on the server
so I want to create immediate redirection from bonesroot.com/beer to beeraddon.com
is that possible or will it affect the server?
this video explains what I want to do
https://www.youtube.com/watch?v=cRm6deeeTVY
and here is the code they recommend
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$ [NC]
RewriteCond %{REQUEST_URI} ^/addonfolder/(.*)$
RewriteRule ^(.*)$ - [L,R=404]
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$ [NC]
RewriteCond %{REQUEST_URI} ^/addonfolder/(.*)$
RewriteRule ^(.*)$ - [L,R=404]
This is the right idea, but it only triggers a 404. To redirect from https://root.example/addon.example/foo to https://addon.example/foo you would need to do it like this:
# Redirect requests to the subdirectory the addon domain points to
RewriteCond %{HTTP_HOST} ^(www.)?root\.example$ [NC]
RewriteRule ^(addon\.example)(?:$|/(.*)) https://$1/$2 [R=301,L]
This assumes that the subdirectory /addon.example is the same as the name of the addon domain, as described initially in your question. (However, for some reason, you have changed this convention later in your question?! *1)
The $1 backreference contains the subdirectory name (the same as the name of the addon domain). The $2 backreference contains the URL-path less the initial slash prefix.
The RewriteCond directive that you previously had that checked against the REQUEST_URI server variable is not required as this check is better performed in the RewriteRule directive itself.
Test first with a 302 (temporary) redirect to avoid caching issues.
*1 If the name of the subdirectory is different to the name of the addon domain then you will need to hardcode this instead. For example:
# Redirect requests to the subdirectory the addon domain points to
RewriteCond %{HTTP_HOST} ^(www.)?root\.example$ [NC]
RewriteRule ^addon-directory(?:$|/(.*)) https://addon.example/$1 [R=301,L]
TIP: Addon domains (cPanel?) don't need to point to subdomains that point to subdirectories off the main domain. They can point anywhere... including areas outside of the main domains document root. This would avoid having to implement these redirects to begin with.
OK I will explain again. I will use fake domains in this case but very similar to my actual domains
The root domain is alter.org
addon domain is numero.com
numero.com files reside inside alter.org/numero/ folder
I want to keep my current redirects which are also
numero.alter.org/foo which redirects to numero.com
what I have in htaccess is this
RewriteCond %{HTTP_HOST} ^numero\.alter\.org$ [OR]
RewriteCond %{HTTP_HOST} ^www\.numero\.alter\.org$
RewriteRule ^(.*)$ "https\:\/\/www\.numero\.com\/$1" [R=301,L]
and I want to add also redirect which redirects
alter.org/numero/foo to numero.com/foo
because I saw one google search result like that and it is duplicate content...Immediately when google bot hits the alter.org/numero/foo it needs to be redirected to numero.com/foo
Please tell me how to add a new redirect to the existing one

How can I stop processing of non-existent domains?

Our website is allowing any prefix/subdomain before the domain.
So if our site is www.domain.com, then the server is allowing;
www.anything.domain.com, where 'anything' can be literally anything, and it displays whatever is on the page that actually exists.
So, www.anything.domain.com/something.php displays the content that should only be accessible via www.domain.com/something.php.
Is there any way using .htaccess to stop this from happening, or redirect it to the version that does actually exist?
Or does this need to be done on the server?
Does anyone know why this is being allowed?
Ideally, this should be configured in server configuration files (also, you can configure DNS to simply not resolve unwanted hostnames, but that is for another question probably).
If you don't have access to server configuration, you can do it in .htaccess:
# to block access if any domain except example.com or www.example.com was used
RewriteEngine On
RewriteCond %{HTTP_HOST} !=www.example.com
RewriteCond %{HTTP_HOST} !=example.com
RewriteRule ^ - [F]
or
# if any domain except example.com or www.example.com was used,
# redirect the request to www.example
RewriteEngine On
RewriteCond %{HTTP_HOST} !=www.example.com
RewriteCond %{HTTP_HOST} !=example.com
RewriteRule ^ http://www.example.com%{REQUEST_URI} [R=301]

htaccess configuration directories for subdomains and root domain

My folders in my www directory as set up as follows:
www/forums
www/helpdesk
www/www
In the base www folder (not www/www), I have the following set up in my htaccess file to redirect based on subdirectory
# Direct subdomains to appropriate folder in WWW directory
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
I also have the following set up for configuring the index.php on the main site (www.example.com) - this is also in the htaccess file in the www directory:
# Rewrite rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ www/$1 [L,QSA]
The problem I am facing is that I have stuff from forums.example.com that I want to embed in www.example.com (Vanilla Forums + WordPress plugin) - if I configure this using the admin panels, the iFrame gets blocked because they are different domains.
I found out that when I go to www.example.com/forums, I get the same front page as forums.examples.com - but all the clean URLs break.
When I look at the .htaccess file in the www/forums folder, I see the following configuration
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php\?p=$1 [QSA,L]
What do I need to do to the .htaccess file in the www directory so that when I go to www.example.com/forums, it works the same as if I went to forums.example.com
...the following set up in my htaccess file to redirect based on subdirectory
# Direct subdomains to appropriate folder in WWW directory
Aside: That snippet is just a canonical non-www to www redirect. It doesn't "redirect based on subdirectory" nor does it "direct [any] subdomains". (?)
the iFrame gets blocked because they are different domains.
It sounds as if you need to set an Access-Control-Allow-Origin HTTP response header on forums.example.com to allow the content to be "embedded" in example.com? Something like the following (using mod_headers) in the www/forums/.htaccess file:
Header set Access-Control-Allow-Origin "http://example.com"
What do I need to do to the .htaccess file in the www directory so that when I go to www.example.com/forums, it works the same as if I went to forums.example.com.
How do "all the clean URLs break"? However, given the HTTP header mentioned above, you shouldn't have to do anything more and still access forums.example.com (not the subdirectory).
Incidentally, the fact you can access the subdirectory (that the subdomain points to) is just how your hosting is configured. Normally you should block access to the subdirectory in order to prevent duplicate content issues (and any other issues, such as the linking problem you mention).
Also note, that due to the way mod_rewrite directives are inherited (or not in this case). The mod_rewrite directives in the www/forums/.htaccess file completely override the mod_rewrite directives in the parent folder.

htaccess for root pages / but not sub-directories or sub-domains

trying to rewrite URL strings only for pages that reside in the root, removing the page extension for cosmetic reaons. For example:
www.site.com/page.html ==> www.site.com/page
www.site.com/about.html ==> www.site.com/about
Using this code currently:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([^.]+\.com) [NC]
RewriteRule (.*) http://www.%1/$1 [R=301,L]
BUT, I don't want the rule to modify sub-directiories or sub-domains. For example I also have:
clientA.site.com (which is mapped to www.site.com/clientA), which I need to not be remain unchanged. Right now it is sending that page (client.site.com/index.html) to a page not found.
Your rule doesn't affect subdomains. There must be other rules causing the 404 not found response.
If you want to restrict requests to the root pages, you can specify that with an appropriate pattern by excluding slashes in the request URL path
# exclude directories
RewriteCond %{REQUEST_FILENAME} !-d
# allow top level request URLs only
RewriteRule ^[^/]*$ http://www.%1/$0 [R,L]
Never test with 301 enabled, see this answer
Tips for debugging .htaccess rewrite rules
for details.

simple subdomain set up for local development (htaccess?)

I am trying to set up a website, domain
examplesproject.co.uk
with a subdomain which is for the moment called
sub.examplesproject.co.uk .
I am with Bluehost and so I have set up the subdomain and I have got the same document root both for examplesproject.co.uk and sub.examplesproject.co.uk but I want content for
sub.examplesproject.co.uk
to actually be located at
examplesproject.co.uk/sub .
So why didn't I set up the document root at examplesproject.co.uk/sub for the sub-domain? Because then, in local development I would need to treat the two domains as completely separate and that would mean no relative urls which seemed silly given that the subdomain folder is just tantalisingly inside the main domain.
However, if someone browsed to sub.examplesproject.co.uk they would get the same content as examplesproject.co.uk, which I don't want, so I set up htaccess rewrite in the root folder like so:
#rewite sub-domain to sub directory
RewriteCond %{HTTP_HOST} ^[www\.]*sub.examplesproject.co.uk [NC]
RewriteCond %{REQUEST_URI} !^/sub/.*
RewriteRule ^(.*) /sub/$1 [L]
That works, however if you browse to examplesproject.co.uk/sub you can still see the content and I don't want to have two locations for the same content. However if I rewrite this sub-directory to show the sub-domain in the browser address, then I create a loop where it keeps feeding round.
As another side-point, I want my main site to be forced to use www, so I also have the following in my root htaccess..
#force add www on main domain
RewriteCond %{HTTP_HOST} ^examplesproject.co.uk$
RewriteRule ^(.*)$ http://www.examplesproject.co.uk$1 [R=301,L]
However, I want to force the sub domain not to have a www infront i.e. http://sub.examplesproject.co.uk and NOT http://www.sub.examplesproject.co.uk. To do this I am trying the following but it doesn't seem to work for other directories within the sub-domain (if that makes sense). Anyhows this is the code which I put in the sub directory (ie at examplesproject.co.uk/sub):
#force remove www on sub-domain
RewriteCond %{HTTP_HOST} ^www.sub.examplesproject.co.uk [NC]
RewriteRule ^(.*)$ http://sub.examplesproject.co.uk/$1 [L,R=301]
Thought I'd mention in case it affects things.
So, my question is, how do I achieve a nice set-up where:
I can use relative URLs for developing and implementing my subdomain.
browsing to http://sub.examplesproject.co.uk shows the content of http://www.examplesproject.co.uk/sub
browsing to http://www.examplesproject.co.uk/sub doesn't duplicate the sub-domain by showing the content (for SEO purposes)
and also
Main domain examplesproject.co.uk is forced to use www - http://www.examplesproject.co.uk.
Sub domain sub.examplesproject.co.uk is force NOT to use www - http://sub.examplesproject.co.uk.
If anyone can help, I would be really grateful. By the way, locally I have set up virtual hosts http://examplesproject and http://sub.examplesproject using wamp and hosts file to replicate the online behaviour.
Thanks alot for reading. Answers/suggestions welcome.
Sorry about that Tim Post! I have put the content in this time! Nice one for looking at this. Hope that this helps someone.
This is the solution that worked for me (thanks to Jim (jdMorgan) at webmasterworld for this - http://www.webmasterworld.com/apache/4254301.htm)..
Put all of these rules, in this order, into the root .htaccess:
# Externally redirect direct client requests for test subdomain subdirectory paths to the test subdomain
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /sub/([^\ ]*)\ HTTP/
RewriteRule ^sub/(.*)$ http://sub.examplesproject\.co\.uk [R=301,L]
#
# Externally redirect all non-canonical, non-blank, non-test-subdomain hostname requests to canonical "www" main domain
RewriteCond %{HTTP_HOST} !^(www\.examplesproject\.co\.uk)?$
RewriteCond %{HTTP_HOST} !^([^.:]+\.)*sub\.([^.:]+\.)*examplesproject\.co\.uk [NC]
RewriteRule ^(.*)$ http://www.examplesproject.co.uk$1 [R=301,L]
#
# Externally redirect non-canonical subdomain hostname requests to canonical test subdomain
RewriteCond %{HTTP_HOST} ^([^.:]+\.)*sub\.([^.:]+\.)*examplesproject\.co\.uk [NC]
RewriteCond %{HTTP_HOST} !^sub\.examplesproject\.co\.uk$
RewriteRule ^(.*)$ http://sub.examplesproject.co.uk/$1 [R=301,L]
#
# Internally rewrite sub-domain requests to subdirectory path
RewriteCond %{HTTP_HOST} ^sub\.examplesproject\.co\.uk$
RewriteCond $1 !^sub/
RewriteRule ^(.*)$ /sub/$1 [L]
Checking THE_REQUEST in the now-first rule prevents the infinite redirection loop problem you encountered.
Note that exact hostnames are now enforced due to the very-careful use of case-sensitivity and anchoring.
Nice one aiit!

Resources