I've read some stuff on this but cannot get it clear (or to work):
I have a domain (call it domain1.co.uk) registered with namesco, but pointing to web hosting elsewhere - the name servers are set to those of the hosting company.
With the hosting company, I have a number of different websites in folders under public_html.
I have another domain (call it domain2.co.uk) also registered by namesco, that redirects to one of the websites mentioned above (ie www.domain2.co.uk forwards to www.domain1.co.uk/thisparticularwebsite/home.php).
What I want to do is get a subdomain on domain2 (sub.domain2.co.uk) to reference a specific php script in thisparticularwebsite (ie www.domain1.co.uk/thisparticularwebsite/sub.php).
namesco tell me I can do this by creating an A record or a CNAME record, but it can only reference the top level domain (public_html at domain1), not a sub-directory or script.
What I think need to do therefore is use .htaccess as follows in the root directory of domain1.co.uk
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub\.domain2\.co\.uk [NC]
RewriteRule ^.*$ http://www.domain1.co.uk/thisparticularwebsite/sub.php [R-301]
or possibly
....
RewriteRule ^.*$ /thisparticularwebsite/sub.php [R-301]
and then create a CNAME record on domain2 for sub (ie sub >> domain1.co.uk).
Is that right?(it doesn't seem to work, though it is unclear what is going on as DNS updates may not have propagated fully yet).
One thing I am not sure if is what should actually turn up in HTTP_HOST - is it the string as typed in by the originating client, or does it get modified along the way by the CNAME records?
.htaccess code should be
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub\.domain2\.co\.uk$ [NC]
RewriteRule ^.*$ http://www.domain1.co.uk/thisparticularwebsite/sub.php [L,R=301]
Related
Upon request I have reformulated my question for clarity so others might benefit better from the answers.
I have 3 domains for the company I work for:
bizwizprint.com (the main website that is hosted on a shared server)
bizwizsigns.com (secondary domain with no hosting attached)
boatwiz.com (tertiary domain with no hosting attached)
The goal is to get my second and third domains to redirect to the first domain onto their own respective landing pages.
First Step: At the domain registrar, change the DNS "A Records" of the second and third domains to resolve to the same IP address that the main website is hosted on.
Second Step: Create a "Site Alias" on the main website server for the second and third domains, they will point to the root directory where the main website files reside.
Third Step: Create or edit an .htaccess file that will redirect the requests for the second and third domains and point them to the landing pages that I have created for them.
The question: What rules do I add to htaccess?
Essentially, I would like to have a user type in "boatwiz.com" in the address bar and the browser will literally GO TO "bizwizprint.com/boatwiz.html".
Please note: I do not want any rewrite rules that will change the actual URL to boatwiz.
The reason for this is that it is a temporary thing. Eventually there will be an actual "boatwiz" website and "bizwizsigns" website and they will most likely be very different in structure. I don't want it to appear that I have three domains with all the same content, or have people make any bookmarks that I will need to redirect yet again in the future.
"How do I redirect an external domain (boatwiz.com) to land in a specific page of a new domain (bizwizprint.com/boatwiz.html) without any rewriting?"
So you probably mean that you want an "internal redirect", not the "external redirect", right? I.e. you want e.g. the bizwizsigns.com to stay displayed in the browser Location bar, but show the contents
of bizwizprint.com/signs, right?
1. htaccess only - impossible
Well, using only .htaccess, this is impossible, because the different domain will force the external redirect. Citing the docs:
Absolute URL
If an absolute URL is specified, mod_rewrite checks to see whether the hostname matches the current host. If it does, the scheme and
hostname are stripped out and the resulting path is treated as a
URL-path. Otherwise, an external redirect is performed for the given
URL. To force an external redirect back to the current host, see the
[R] flag below.
2. Iframe trick
What you could do is to use iframe. Put this code at bizwizsigns.com/index.html:
<iframe src="http://bizwizprint.com/signs" width="100%" height="100%"
style="border: 0 none;" frameborder="0">
But there are many downsides of this solution:
the URL will not change in the browser's location bar, even if user clicks within the iframe
browser bookmarks and history will not work as expected.
3. Clever hosting setup (domain aliases into the same dir)
Are you in a hosting environment, or do you have your own server? Sometimes the hosting allows you to make aliases of several domains that are handled by the same local directory tree. In that case, you won - you can write .htaccess so that it handles the requests as internal redirects:
RewriteCond %{HTTP_HOST} bizwizsigns\.com$ [NC]
RewriteRule ^$ /signs [L]
RewriteCond %{HTTP_HOST} boatwiz\.com$ [NC]
RewriteRule ^$ /boats [L]
which will (internally) redirect bizwizsigns.com to /signs (= your content of bizwizprint.com/signs, because you have one hosting server directory for all 3 domains). But if you e.g. want all queries like bizwizsigns.com/<foo> to be redirected to bizwizprint.com/signs/<foo>, you have to be more careful - see the added condition on REQUEST_URI to prevent endless loop:
RewriteCond %{HTTP_HOST} bizwizsigns\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/signs/ [NC]
RewriteRule ^.*$ /signs/$1 [L]
Assuming that you have all 3 domains pointing to the same document root, you just need this in its htaccess file:
RewriteCond %{HTTP_HOST} bizwizsigns\.com$ [NC]
RewriteRule ^$ http://bizwizprint.com/signs [L,R=301]
RewriteCond %{HTTP_HOST} boatwiz\.com$ [NC]
RewriteRule ^$ http://bizwizprint.com/boats [L,R=301]
RewriteCond %{HTTP_HOST} (bizwizsigns|boatwiz)\.com$ [NC]
RewriteRule ^(.+)$ http://bizwizprint.com/$1 [L,R=301]
So if it's just http://bizwizsigns.com/ or http://boatwiz.com/, then you get redirected to http://bizwizprint.com/signs or http://bizwizprint.com/boats. But if you have anything after the last /, like http://bizwizsigns.com/foo/bar.html then you'll get redirected to http://bizwizprint.com/foo/bar.html.
I am setting up two wordpress sites on one shared hosting plan for a client. 123-reg says on the decription that many sites can be hosted on the same server so I thought this would be a simple click of a button thing but clearly not.
I have two sites sitting in 2 separate sub directories; FolkstockFestival and FolkstockArtsFoundation. I also have two domains www.folkstockfestival.com and folkstockartsfoundation.com. I need each domain to go to its corresponding sub directory.
I thought you'd be able to do this with the DNS settings but I haven't found a way to do that so I think .htaccess is the way forward. With mod_rewrites, is there a way to direct the user to the right directory depending on which domain is requested? The .htaccess file would be in the root directory.
I do not want the sub-directory to show on the domain so www.folkstockfestival.com shows as is but directs to the correct site.
Many thanks
Use DNS to make both of your domains pointing on your server, and use this htaccess :
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} www\.folkstockartsfoundation\.com [NC]
RewriteRule ^(.*)$ /FolkstockArtsFoundation/$1 [L]
RewriteCond %{HTTP_HOST} www\.folkstockfestival\.com [NC]
RewriteRule ^(.*)$ /FolkstockFestival/$1 [L]
My very dear Stackoverflow community,
I have the following redirection problem and after several unsuccessful attempts I come here in search of enlightenment. My problem is the following. I have a domain, let's call it 'www.mydomain.com', and my 'public_html' directory has two folders as follows:
public_html
public_html/my_app/
public_html/my_other_app/
First, I would like that when typing the URL 'www.mydomain.com', I get redirected to the contents of folder 'my_app', while keeping the same URL. In fact this I have already accomplished, so whenever I type 'www.mydomain.com' I get redirected to 'www.mydomain.com/index.php', which actually corresponds to the 'public_html/myapp/index.php' script under 'myapp'.
Now I want to have a subdomain called 'other.mydomain.com', which has to redirect to contents of the 'my_other_app' folder, but I do not know how to make .htaccess work for this and at the same time work for the first case also.
So this is basically, the main domain redirects to one folder, and a subdomain redirects to another folder, and both folders are located under the public_html directory
Any hints more than welcome.
For your reference I post below my current .htaccess file:
RewriteEngine On
# redirect to www prefix
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L]
# if start with www and no https then redirect
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www.mydomain\.com$ [NC]
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L]
# rewrite URL to trim folder
RewriteCond %{REQUEST_URI} !^/test/
RewriteRule ^$ /login [L,R=301]
RewriteRule ^(.*)$ test/$1 [L]
This actually works for my main domain, it also rewrites the url to https. I need to add something in here in order to process separately the 'other.mydomain.com' and redirect to the '/my_other_app/' subfolder
what you need is a vhost (virtual host) per app. In the vhost, you will define the vhosts root directory, which will point to either of your sub directories.
There is IP based vhosts (one IP address per subdomain) or name based vhosts (the vhost is chosen based on the HTTP host header that all modern browser send).
But there is too much to say about vhosts to write it all here, just read the apache documentation here:
http://httpd.apache.org/docs/2.2/vhosts/
I think with pure .htaccess files, you can't do that (I might be wrong). Normally you would add vhosts in the main apache config. Based on your hosting, this may not be possible. Talk to you hosting provider in that case.
Marc
I have two working domains on the same server.
How can I forward all traffic to all files on the old temporary domain to the new correct domain?
Seems like this might be an SEO issue?
But, since the temporary testing domain isn't registered, would it even be crawled?
Is this even a problem?
I'm currently using:
RewriteEngine On
# Force to WWW
RewriteCond %{HTTP_HOST} ^thecorrectdomain\.com
RewriteRule (.*) http://www.thecorrectdomain.com/$1 [R=301,L]
#
The incorrect domain is the setup that we used for testing, before we pointed the domain name to the new server.
The second is the correct domain.
The incorrect testing domain looks like:
http://1234.hostingcompany.com/~username/index.php
The intended correct domain looks like:
http://thecorrectdomain.com/index.php
Or is this something that the hosting company has to do at their level?
Assuming hostingcompany supports .htaccess. You create a .htaccess in http://1234.hostingcompany.com/~username/ which contains something like:
RewriteEngine On
#might not be necessary
RewriteBase /~username
RewriteRule .* http://thecorrectdomain.com/$0 [R=301]
PS while testing use 302, instead of 301.
Hey, My host is absolutely terrible. For some odd reason creating a subdomain in cPanel simply does not work, and their support lines are always busy. I thought I could get around this by using .htaccess. I'm sure it's not that hard, but I'm kind of new to mod_rewrite and have had little success searching in the last 5 hours. Heres the situation:
/home/user/public_html automatically redirects to http://www.example.com
Since I'm using a CMS in public_html it has already added the rule in .htaccess to redirect anything unfamiliar after example.com/ to a 'Page Not Found'
/home/user/subdomain needs to redirect to http://subdomain.example.com
How should I go about creating a subdomain redirection to an absolute path? Or How can I add an exception in my .htaccess
I doubt you'll be able to get your subdomain to function outside of your public_html folder (although I'm no server admin). Typically that requires DNS modifications or tweaking the server's configuration. Have you tried making a sub-directory and rewriting calls to the subdomain? For example this placed in the .htaccess within your public_html directory:
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$
RewriteRule (.*) /subdomain/$1 [L]
I'm not sure if that would work (never needed to test it myself), but it's more likely to function than trying to target files that live outside the directory specified by the webhost as the location of your domain's files.
Good luck!
Try this rule:
RewriteCond %{REQUEST_URI} !^/home/user/
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$
RewriteRule ^ /home/user/%1%{REQUEST_URI} [L]
But your webserver already needs to be configured so that every request of foobar.example.com gets redirected to this specific virtual host.