Using .htaccess to redirect all requests from subdomain to main domain - .htaccess

I have a hobby website for a number of different projects and want each project to have it's own subdomain, like foo.domain.com, bar.domain.com etc.
I use Drupal with the Domain Access module, meaning all subdomains should point to the base installation of Drupal, and then the module recognizes what subdomain the request comes from and serves a page according to that.
Now, since this is just a hobby project, I keep it on a free shared hosting account, which means a few limitations:
No wildcard subdomains.
Each subdomain is linked to a subdirectory with the same name, for example domain.com goes to /public_html/ and sub.domain.com goes to /public_html/sub/ The hosting forces this.
I can't create symlinks.
I have limited space and databases, meaning I can't just make a new installation for each project. (Hence the Domain access module)
My domain registrar (Godaddy) doesn't play nice with shared hosting. I tried hosting the DNS with them and doing a wildcard A record to my hosting server, but it didn't work, and Godaddy don't allow wildcard CNAME records for some reason...
It seems the only option left for me is some .htaccess magic.
I need a .htaccess file to put in the subdomain director(y/ies) to tell apache:
The data is in the root web directory
To not change anything else, so that the Drupal module knows what subdomain was requested and the user still sees "sub.domain.com" in the browser window.
Thankful for your help!
TL;DR
How can I tell Apache to use the data from another directory (i.e. /public_html/ instead of /public_html/subdomain/) WITHOUT making a redirect or any changes to the headers? HTTP_HOST needs to be intact.
Thanks!

Try this:
http://kb.mediatemple.net/questions/242/How+do+I+redirect+my+site+using+a+.htaccess+file%3F

try this
RewriteEngine On
RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com%{REQUEST_URI} [R=301,L]

try this.
RewriteEngine On
RewriteCond %{HTTP_HOST} sub\.example\.com$ [NC]
RewriteRule ^sub/(.*)$ $1 [L]
Not sure if it will work, because I don't know how your host configured to server to map the subdomains to a different folder.
Otherwise you could try the Proxy flag, but that will not set the correct http_host variable in php.
Are you sure Drupal doesn't have a different method of doing multiple installs. I know WP did have a option to use a prefix for all table-names so multiple installs can coexist, using the same database, as long as they use different prefixes. Not sure how big a Drupal install is, and what amount of diskspace your host provides.

Related

Redirect all accessible URL's to only one specific subdomain

I have my working worpress domains, subdomain and alias working normally and locate at root web server "www/".
htpps://domain.com
htpps://domain.net
htpps://en.domain.net
Later I decide to install a support web application in same server in folder "www/support/".
Setting this support app I needed to set one domain or subdomain, and I set "support.domain.com".
After last step I realise that all the following URL's could access to the support app:
htpps://domain.com/support
htpps://domain.net/support
htpps://en.domain.net/support
htpps://support.domain.com/support
htpps://support.domain.net/support
Since this support app doesn't work correctly with differents domains or subdomains from the main one that was set initially ( that was: "support.domain.com" ), I'm trying to redirect all to one unique subdomain: "support.domain.com".
The file ".htaccess" locate in support app folder ("www/support/.htaccess") still empty, all rewrite rules I try didn't worked, due my low knowledge.
How could I redirect all accessible URL's to only one "support.domain.com"?
or
Block all access to this folder "www/support/index.php" that are not "support.domain.com"?
I found and try lots of similar questions but none work correctly or give too many redirections.
in support.htaccess, you can use the following rule
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.+ http://support.domain.com/ [L]
After several time pass over, I finally locate the problem, double quotes was “ instead of "
This small details was creating problem in every .htaccess file in my host, after this correction all working correctly and it reach support/.htaccess now.

HTACCESS: How to URL rewrite a sub-subdomain?

I have the subdomain apps.domain.com pointing to domain.com/apps
I would like to URL rewrite appName.apps.domain.com, where appName is the variable pointing to domain.com/apps/appName
I'm looking for an internal forward (no change in the browser's URL)
What should I have in my .htaccess file and where should I save it? Within /apps/ folder? Or within the root folder?
Lastly, if it is not possible with an .htaccess file, how can I do this? I'm using a Linux godaddy server as host.
**** Question updated 07/08/2018; added more details ****
I am using GoDaddy SHARED hosting
It is currently hosting multiple domains
Thus, the directory structure is public_html/domain.com/, where /domain.com/ is the name of the hosted domain name(s)
The sub-subdomain is exclusive to 1 specific domain
Example:
If the domain name is domain.com
There's multiple names, but to give an example. if the name of the app is awesome
If the uri is: awesome.apps.domain.com will point to...public_html/domain.com/apps/awesome/
If the uri is: ubertz.apps.domain.com will point to...public_html/domain.com/apps/ubertz/
And so on...
I'm using this code for subdomain forwarding:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.apps\.domain\.com$
RewriteRule (.*) /apps/%1/$1
Explanation:
In RewriteCond you catch app name using regular expression (.*) - it will be saved to variable %1
In RewriteRule you forward everything - that's another (.*) expression which content will be saved in variable $1 - to the directory /apps/<appName>/<path after URL>
For more information about Regular Expressions I recommend to check this tutorial: http://www.webforgers.net/mod-rewrite/mod-rewrite-syntax.php
You should save your .htaccess in the root directory of the domain, in your case in the public_html/domain.com/. If it doesn't work, place it in the apps folder.
Create wildcard sub-subdomain
The first thing that you need to do is to create a wildcard *.apps.domain.com sub-subdomain.
(If you're using Nameservers, skip this step!) Log in to your domain name registrar, and create an A record for *.apps.domain.com (yeah, that's an asterisk) and point it to the server IP address. Note that DNS can take up to 48 hours to propagate.
Log in your web hosting account and go to the menu 'Subdomains' under Domains section. Create a Subdomain *.apps.domain.com that's pointed to the "/public_html/domain.com/apps" folder as its Document Root. And wait until the propagation is over.
Visit How to create wildcard subdomain in cPanel and How to Create a Sub-Subdomain for more info. If your server's configuration didn't allow you to create a wildcard sub-subdomain then just create *.domain.com and point it to "/public_html/domain.com/apps".
Rewrite wildcard sub-subdomain
Place these directives in the .htaccess file of the document root of the wildcard *.apps.domain.com which in this case is "/public_html/domain.com/apps".
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.+)\.apps\.domain\.com
RewriteRule (.*) http://domain.com/apps/%1/$1 [QSA,P,L]
The %1 is the numbered backreference of (.+) and the $1 is of (.*). You need to include the [P] flag if you URL rewrite from a specific domain http://domain.com/apps/%1/$1 in the same server, without that it will be redirected. Use the [QSA] flag if you'll going to use the query string of a rewritten wildcard subdomain. Visit P|proxy and QSA|qsappend for more info about them. And just tweak some adjustment if I forgot something, other than that, is the server's fault.
Few things to consider
You must configure your .htaccess file for the duplicate subdomains that will be going to exist, such the otherwildcard.apps.domain.com, another.wilcard.apps.domain.com and the other.deep.level.apps.domain.com that will duplicate apps.domain.com as they're all have the same document root. Configure to redirect or to tell to the client that those subdomains aren't exist.
Try these .htaccess directives:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^(.*)\.apps\.domain\.com$
RewriteRule (.*) /apps/appName/$1 [P,L]
The RewriteRule will not redirect the user because of the [P] flag. This will redirect the request to the mod_proxy module which handles the request and returns the result.
See http://httpd.apache.org/docs/2.4/mod/mod_proxy.html for more information.

Masking sub domain with a new domain while preserving the paths

I own a domain since long, just masking the names:
http://mydomain.com
Later I started using a subdomain on this domain for some project.
http://subdomain.mydomain.com
Those projects grew and now I have a structure like
http://subdomain.mydomain.com/project1
http://subdomain.mydomain.com/project2
http://subdomain.mydomain.com/project3/subproject1
http://subdomain.mydomain.com/project3/subproject2
http://subdomain.mydomain.com/project3/subproject3
http://subdomain.mydomain.com/project4
....
etc.
now I bought a new domain (shortdomain.com) where I plan not to move anything but everything should be accessible via redirects so everything looks like:
http://shortdomain.com
http://shortdomain.com/project1
http://shortdomain.com/project2
http://shortdomain.com/project3/subproject1
http://shortdomain.com/project3/subproject2
http://shortdomain.com/project3/subproject3
http://shortdomain.com/project4
...
etc.
So basically I need to do two things:
1. if anyone visits my old domain, redirect them the new naming structure. i.e. if someone loads http://subdomain.mydomain.com/project2 they should be redirected to http://shortdomain.com/project2
when a user loads/redirected to http://shortdomain.com/project2 this should actually load the content present at http://subdomain.mydomain.com/project2
So I will not manually migrate projects,codes and GBs of other data. I think this might be acievable by smart redirection only.
Just FYI:
1. I have full DNS control of both the domains
2. I am hosted on hostgator
3. I use cloudflare on the first domain and would like to continue using it
I think this might be acievable by smart redirection only.
No, redirection changes what's in the browser's location bar. If you redirect to shortdomain.com then the request will get sent to shortdomain.com, and have nothing to do with subdomain.mydomain.com anymore. If you redirect back to subdomain.mydomain.com, then the location bar in the browser will change as well.
What you really want to do is point shortdomain.com to the same server and document root that subdomain.mydomain.com is on. Then use this to redirect (either in htaccess file or server config):
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain\.mydomain\.com$ [NC]
RewriteRule ^(.*)$ http://shortdomain.com/$1 [L,R=301]
If, for whatever absurd reason you can't point the shortdomain.com DNS to the same webserver that serves subdomain.mydomain.com, or can't setup that webserver to accept requests for the shortdomain.com host, you need to setup a proxy server. And it'll work something like this:
2 Webservers, server A (hosts subdomain.domain.com) and server B (hosts shortdomain.com)
Someone requests http://subdomain.mydomain.com/project3/subproject1
server A gets the request and redirects the browser to http://shortdomain.com/project3/subproject1
browser's location bar changes to new location
server B gets the request and reverse proxies the request back to server A
server A gets the request again but must recognize that it is a proxy and then serve the page instead of redirecting
As you can see, this is a horrendously ineffecient solution. It's also a high possibility that your hosting service won't allow you to setup proxy servers.
I have full DNS control of both the domains
With full control I assume you can enable mod_proxy as well on Apache web-server of shortdomain.com. Once that is done set it all up this way.
On subdomain.mydomain.com enable mod_rewrite and place this rule in Apache config OR DOCUMENT_ROOT/.htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain\.mydomain\.com$ [NC]
RewriteRule ^ http://shortdomain.com%{REQUEST_URI} [L,R=301]
On shortdomain.com enable mod_proxy, mod_rewrite and place this rule in Apache config OR DOCUMENT_ROOT/.htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^shortdomain\.com$ [NC]
RewriteRule ^ http://subdomain.mydomain.com%{REQUEST_URI} [L,P]

TYPO3 multidomains for developing

I use a developer domain (domain.com.dev) for my local installation. For that I created three domain records for one domain:
domain.com -> redirect to http://www.domain.com
www.domain.com
domain.com.dev
But to get domain.com.dev working I have to deactivate the first two one. Will be there an other solution to do that with constants or pageTSconfig?
edited:
Using now htaccess-redirect for non www.
# Redirect non-www to www and ignore dev subdomain.
RewriteCond %{HTTP_HOST} !(www\.|dev\.).* [NC]
RewriteRule .* http://www.%{HTTP_HOST}/%{REQUEST_URI} [L]
Above I forgot another subdomain, so the domain records now as followed:
First tree:
www.domain.com
dev.domain.com
Second tree:
sub.domain.com
dev.sub.domain.com
But if I call now dev.domain.com the links are parsed as www.domain.com. Before I used baseURL. Than I read somwhere, I can't remember where, that baseURL is outdated.
But if I don't use baseURL this trick will not work. And with sub.domain.com it will not work anyway.
edited II
I' have now following two domain records for the domain sub.domain.com:
sub.domain.com
dev.sub.domain.com
If only one is activated, the internal links will be like href="home.html", if the second is activated they are href="http://sub.domain.com/home.html". But I'm currently on the domain dev.sub.domain.com.
Cause I have more domains in one installation I need these records. But how can I get rid of this prefixed URLs?
If TYPO3 prepends the domain to the links, baseURL will be useless...
In my experience it is not a good idea to structure it that way. It is much easier using dev.domain.com for your dev system and then add this to the hosts file to point to your local system.
The non-www to www redirect should also be made with a .htaccess, not within TYPO3 (performance). To solve the problem with your current structure you could try to remove the domain.com from the list and do then redirect as mentioned. Then the .com.dev should also work.
If I'm understanding correctly you need to setup a baseUrl based on the new domains.
This can be done as follows in TypoScript:
[globalString = ENV:HTTP_HOST=dev.domain.com]
config.baseUrl = http://dev.domain.com/
[globalString = ENV:HTTP_HOST=dev.sub.domain.com]
config.baseUrl = http://dev.sub.domain.com/
[end]
Next, if you use RealUrl or CoolUri, you will also need to create domain-records for these in the Typo3 backend.
Hope this helps :)

Need to redirect to true root folder

I am running a website on MAMP, and the root is http://localhost/sandbox
When I have links that link to, for example - /calendar
it directs them to localhost/calendar, I want it to redirect to localhost/sandbox/calendar
What would I have to do in htaccess to get it to redirect everything to localhost/sandbox/ as the root?
You would usually change the application or site that generates the links, and have that add the /sandbox to the URL.
If that's not possible, putting this into the web root directory (the one above /sandbox) should do:
RewriteEngine on
Options +FollowSymlinks
RewriteCond %{REQUEST_URI} !^/sandbox # If URL does not start with /sandbox...
RewriteRule (.*) /sandbox/$1 # Add /sandbox/ in front of it
(if it's easier to achieve with Alias, Apache Gurus, feel free to add your solution, but I couldn't get Alias to work for this scenario.)
However, this solution will make any other directory besides /sandbox inaccessible. You may want to re-think your Virtual hosts structure!

Resources