Redirecting parts of a web page in IIS - 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

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 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