Move resource to different domain based on subdomain - .htaccess

Several companies are sharing resources (wiki, forum, shops...), now they like to use ONE server certificate for this.
The url looks as following:
company1.domain1.com or www.company1.domain1.com
company2.domain2.com or www.company2.domain2.com
company3.domain3.com or www.company3.domain3.com
New pointing to the same newdomain.com/company1 on the hosting.
What I want to achieve at the end is:
newdomain.com/company1
newdomain.com/company2
newdomain.com/company3
In the browser when somebody type www.company2.domain2.com in the URL you should see http://newdomain.com/company2 (without www)
I need two examples. One is exactly this thing I described. Second is the same thing, but at the end in URL I want see https://newdomain.com/company2 (without www)

In the htaccess file in your domain1.com, domain2.com, domain3.com, etc, document root, add:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.[^.]+\.com$ [NC]
RewriteRule ^(.*)$ http://newdomain.com/%2/$1 [L,R=301]
This will redirect http://www.mycompany.somedomain.com/path/to/file.txt to http://newdomain.com/mycompany/path/to/file.txt

Related

Subdomain in .htaccess file only works with index

I have a subdomain setup in my .htaccess, which only seems to work with the default index.html page. I'd LIKE it to work for ANY page in the folder corresponding to the subdomain. Edited for privacy, assume my domain is example.org. The pertinent parts of the file look like this...
#subdomain
RewriteCond %{HTTP_HOST} ^subname\.example\.org$ [OR]
RewriteCond %{HTTP_HOST} ^www\.subname\.example\.org$
# (a few lines added by my hosting company deleted -- see below)
RewriteRule ^/?$ "http\:\/\/example\.org\/subname\/" [R=301,L]
So the result of the above is that if I have an index.html page in my 'public-html' (root?), http://example.org and a different index.html stored in a sub-folder (having the same name as the subdomain), I will get this expected result, which works...
browse to: http://example.org results in viewing http:// example.org/index.html
browse to: http://subname.example.org results in viewing http:// example.org/subname/index.html
Great so far. This is what I expected when I created the domain name. However, given a specific file myfile.html stored in the subname folder, I would expect this to work also, and it doesn't...
browse to: http://subname.example.org/myfile.html results in a 404 error.
This despite the fact that browsing to http://example.org/subname/myfile.html works fine. In that case myfile.html is displayed. So is there anything I can do to modify the subdomain code to get the result I'm looking for? Namely, browsing to http://subname.example.org/ANYFILE should work as well as browsing to http://example.org/subname/ANYFILE, regardless of what 'ANYFILE' is. This, after all, is one of the main reasons I set up the subdomain to begin with!
Note: I confess that I relied on my hosting company's cPanel utility to create the subdomain code, so I asked for their tech support for help first. Long story short they didn't. Maybe what I hoped for is not actually possible?
Also, the lines I deleted' from the code had to do with something called "well-known/acme-challenge", added by my hosting company at some point. Since removing them had no effect on the behavior I've described, I left it out to avoid clouding the issue.
RewriteRule ^/?$ "http\:\/\/example\.org\/subname\/" [R=301,L]
This only "redirects" the document root. To redirect all URLs you need to change the above to read something like:
RewriteRule (.*) http://example.org/subname/$1 [R=301,L]
The $1 backreference refers to the URL-path captured in the RewriteRule pattern, ie. (.*).
No need to backslash-escape the colons, slashes and dots in the substitution string (that's typical of cPanel).
Also, the lines I deleted' from the code had to do with something called "well-known/acme-challenge", added by my hosting company at some point.
Those lines will likely be required when the (Let's Encrypt?) SSL cert auto-renews. (Although the above redirects to "http" - are you not using HTTPS?)
UPDATE:
RewriteCond %{HTTP_HOST} ^subname\.example\.org$ [OR]
RewriteCond %{HTTP_HOST} ^www\.subname\.example\.org$
Just as an aside, these two conditions could be reduced to a single condition if you wanted. For example, the above is equivalent to:
RewriteCond %{HTTP_HOST} ^(www\.)?subname\.example\.org$

Redirect An Unknown Source Domain To Subfolder Using .htaccess

I am interested in offering my users a "white label" service, wherein they will set up a CNAME record that points to my server.
For example, I would like client.theirdomain.com to actually redirect to https://example.com/client/, but keep the URL still as client.theirdomain.com (and, further, if they click a link that is actually https://example.com/client/something.html, it would appear to the user as client.theirdomain.com/something.html.
I attempted to do this in my .htaccess with the following;
RewriteCond %{HTTP_HOST} ^client\.(.*)\.(.*)$ [NC]
RewriteRule ^$ /client [NC,L]
For posterity, I also have a valid subdomain created (client.example.com) that directs to the root of my website, as well as a CNAME record created on a client's server (which points client.theirdomain.com to client.example.com).
When I attempt to use the above settings, however, I receive an error that there were too many redirects.
Thanks!

htaccess redirect of root domain, not subfolders with url masking

I am trying to do the following -
Redirect just the root domain to a different domain.
The redirect needs to be masked so the user still thinks they are on the url they typed.
Existing subfolders should still work with the existing root domain.
For example-
I have an installation using www.currentsite.com which has lots of subfolders for example www.currentsite.com/store
I want to redirect just the root of www.currentsite.com to www.newsite.com but want the browser to still say www.currentsite.com.
If the user goes to www.currentsite.com/subfolder I still want that to work with the original installation.
I have the following which seems to be handling redirecting just the root fine but does not mask the url...
RewriteEngine on
RewriteCond %{HTTP_HOST} www.currentsite\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
Rewriterule ^(.*)$ http://www.newsite.co.uk/ [L,R=301]
Any help id appreciated.
For what you call "masked" the usage of apaches proxy module makes most sense:
ProxyPass https://www.currentsite.com https://www.newsite.co.uk
ProxyPassReverse https://www.currentsite.com https://www.newsite.co.uk
It maps one base url to another one and takes care to transparently and reliably rewrite all contained references.
The proxy module can also be used by RewriteRules, the P flag does that. But in the end it comes out itself and the above, direct usage is more transparent and less complex.
Here is the documentation, as typical for the apache project it is of excellent quality and comes with lots of good examples: https://httpd.apache.org/docs/2.4/mod/mod_proxy.html

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.

URL Rewriting subdomain

I'll be quick on what im trying to do,
basically I have a user profile page that will be my url, let's say,
profile.php?user=alex
So now what is working fine in my .htaccess file is changing that into
website.com/alex
for quicker access.
For other purpose, I would need that to be basically
alex.website.com
but I couldnt figure out a way to rewrite my URL to that, instead of having a subdomain for every user.
If you have any idea if it's possible & how I would go on doing this, I would appreciate it alot!
Thank you
Alex
To just rewrite the URL path, try this rule:
RewriteRule ^[a-z]+$ profile.php?user=$0
If your user names have a different syntax, replace [a-z] as you need.
For rewriting the host, try this rule:
RewriteCond %{HTTP_HOST} ^([a-z]+)\.example\.com$
RewriteRule ^$ profile.php?user=%1
Note that this will only rewrite //alex.example.com/ internally to //alex.example.com/profile.php?user=alex. Additionally, your server will already need to be configured that it accepts any subdomain of your domain (see ServerAlias and name-based virtual hosts).

Resources