Redirect An Unknown Source Domain To Subfolder Using .htaccess - .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!

Related

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.

Redirecting parts of a web page in IIS

I have a "dummy" web site in IIS on a 2008 server that redirects users to the correct web site.
For historical reasons this box, and what everyone knows, is "mail.abc.com".
"mail" has now come to mean several other things, but people just type that name or have it bookmarked and expect to get directed to the "type of service" they are looking for.
mail.abc.com (ports 80 or 443) actually redirects to -> webmail.abc.com (port 443)
webmail contains a webportal for the users to sign into their email, but it also holds their personal web pages. These are accessed by going to ->mail.abc.com/~their_name
I want to create some type of rule that splits this into two pieces (we are splitting these two services onto 2 servers - one for the email part and one for their personal web pages)
so if you go to the generic page the redirect should goto webmail.abc.com
but if there is a "~", I want to redirect to a different location => webpages.abc.com/~their_name
I was looking at HTML redirects to do this, but I'm not sure how to split the typed-in URL, the "root" URL could be easily redirected, but how would I build something for any URL that had the tilde in it?
If you are able to use .htaccess, put a file .htaccess with the following configuration in the root folder of the mail.abc.com host:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/~(.*)
RewriteRule ^(.*)$ http://webpages.abc.com/~%1 [R=302,L]
RewriteCond %{REQUEST_URI} ^(.*)
RewriteRule ^(.*)$ http://webmail.abc.com%1 [R=302,L]
What happens:
Line 2 and 3 is the first set of RewriteCond and RewriteRule. The RewriteRule in the third line will redirect to http://webpages.abc.com/ only, if mail.abc.com was called with ~ in the URL segment. So if I try to open for example http://mail.abc.com/~m_eberhard I will be redirected to http://webpages.abc.com/~m_eberhard.
The next set in line 4 and 5 will redirect everything else to webmail.abc.com. If I try to open mail.abc.com/asdasdasd I will be redirect to webmail.abc.com/asdasdasd

htaccess to redirect submain with sub-pages on another server

I need to redirect my sub-domain http://listings.mywebsite.com/i/ to "mywebsite.com" but the trouble is, this "/i/" and all of the pages following this "/i" are hosted on another server. I created a redirect to direct "listings.mywebsite.com/" to my home page, so no problem there... But this "/i/" at the end is giving my trouble.
I tried several htaccess redirect scripts found within this site but none of them worked, likely because this /i/ are not on my server.
That is, if I go to "listings.mywebsite.com/i/" , I see a "document unreachable" error page because this "/i" somehow is connected to this company's server. However, if I go to http://listings.mywebsite.com , I'm redirected to my homepage.
I asked the company to remove me from their servers, but they are not doing it yet and are being slow if no response at all (something about keeping me active in their servers in case I decide to come back to them but I'm not.
Thanks much for any help!
Frank
Put this code in your DOCUMENT_ROOT/.htaccess file on listings.mywebsite.com host:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^listings\.mywebsite\.com$ [NC]
RewriteRule ^i/(.+)$ http://mywebsite.com/$1 [L,NC,R=301]

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]

Move resource to different domain based on subdomain

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

Resources