ASP.NET sub domain website's with same codes but different in Contents & Images - asp.net

I currently have a website for eg:- www.domain.com & i need to create 3-4 sub domains for this website like
abc.domain.com,xyz.domain.com ...
I want to maintain the same codes for all the subdomains, & for each of the Sub domains i have the same look & feel except the css style, the logo's, the Page title's & few Page contents would be changing.
The titles & images(i will only store the image name) can come from the resource file,
Like for example i will have Resource file for each sub domains & my website should automatically pick the resource file based on the domain.
but not sure which is the best & easy way to achieve this.
Similar Question I have Seen based on UI Culture. I donot want to follow this approach. As i will be using English for all my subdomains & in future i will be having more sub domains.

If you have full-control over your web server and you are using IIS, you can achieve this easily by having multiple sites pointing to the same physical location in the file system. Then, each site can (and must) have a unique binding which will allow IIS to route incoming request to the appropriate site (sub-domain). Note that it is important that you have each site running on its own app pool rather than sharing the same app pool
Furthermore, you need to consider that the scalability of this approach is very limited, if in the future you need to have...say, hundreds of sites it can potentially add a lot of overhead to your web server. So, if you need a scalable solution you should go for a multi-tenant approach instead which you can easily load-balance.
One last note, images bundled up in resource files is a pretty bad idea...avoid it!!!
Site-specific content
Having different sites pointing to the same physical location doesn't mean that they have to share the same content all the time. You can still decide what content to serve dynamically based on the request context by simply inspecting the request object...this goes for dynamic ASP.NET pages, however for static pages you will need to create a virtual directory for each site and point them a location specific to that site only...this will allow you to customize the content for a specific site...see screenshot below how I have a "login" virtual directory which points to somewhere else...
Example
This is a very simplistic example. Suppose you have two sites set up...
en.domain.com
es.domain.com
The sites are pointing to the same physical location...C:\inetpub\domain\site
Then, there's a page index.aspx that has a panel in it that displays a welcome message...
<asp:Panel ID="pnlWelcome" runat="server"></asp:Panel>
for the "en" sub-domain you want to display "Welcome to Our Site" in english but for the "es" sub-domain you want to display "Bienvenidos a Nuestro Sitio" in spanish.
Then, if you set up a virtual directory for each site pointing to a separate location in the file system....say for the "en" domain the location is...C:\sitescontent\en\ and for the "es" domain the location is ...C:\sitescontent\es\, you can then place a user control, xml file, whatever you need inside these site-specific folders with the site-specific content. Then it is as easy as referencing the site-specific content with a virtual path...
string path = MapPath("~/content/{your_site_specific_content}");
or
Control ctrl = LoadControl("~/content/{your_site_specific_content}");
pnlWelcome.Controls.Clear();
pnlWelcome.Controls.Add(ctrl);
If you have a more complex architecture in your application, you would want to inspect the Request object to find out in which context the request is executing whether in the "en" site context or "es" site context....
HttpContext.Current.Request.Url.Host;
By NO means, I'm suggesting this the best way to handle localized resources in your system...it is just an example of how to handle site-specific content.

Related

Prevent Direct Access To files ASP Web.Config

Strange question but hopefully its possible.
we have files in the root of our site (/files) these are uploaded through the main cms side of the website (/admin) and to be accessed by people who have access to the (/school) directory.
At the moment, if you have the direct link URL EDIT TO THE FILE you can gain access to the file
the files folder has its own web.config file with some rules, is there a way i can add some of my own authentication within the web.config to only allow requests that come from the School folder to have access to the files?
In my head i would have a piece of VB code that does something like this:
If (Request.UrlReferrer <> Nothing) Then
If Page.Request.UrlReferrer.ToString.Contains("/School/") = True Then
???
End If
Else
Response.Redirect("index.aspx")
End If
I've tested this in a simple page and it seems to work okay.
Well, I would assume that the users who can use school folder are memembers of a secuirty role called School (or some such).
thus, any user not a member of school can't use the pages (or files) in the school folder.
Since you want the SAME restricitons for the folder files, then use the same web.config to secure the files folder.
That will mean only users who can use the shools folders will also be the same group of people that only use the files folder.
Now the above will not stop members of securty role "school" from typing in any valid URL to the files folder. So, if you want to prevent this, then I would suggest that for all files up-loaded, you create a folder inside of files folder based on their PK row in the membership table (Memebership.ProviderKey).
Then what you would do is turn off all role groups rights to the files folder. I perfer not EVER allowing a direct URL to files.
Remember, only IIS respects the IIS security settings and provider for a web based URL. Code behind is 100% free to load, read, write, use, play with ANY file via code behind. Code behind uses plain jane FULL path names - and those path names and file names used by code behind does NOT care nor use IIS security settings. Only web based URL's dish out by IIS respect and use the IIS security settings you have for the schools folder. So, copy the web.config for schools over to files, and you are done.
But, code behind? it does not care nor use those settings at all.
So, code behind does NOT use nor respect nor care about IIS security settings. IIS role membership ONLY applies to the asp.net pages and web based URL's. But, code behind is a 100% free to get and grab any file on the system - even files outside of the wwwroot path name of the web site.
So, as a 2nd level and better security?
I would not allow ANY URL's to the files folder.
You then display a grid or list of files on the web page, and code behind for the download button can then go read/get the file (you can use TransMitFile to download that file)
So, best approach is to not allow ANY valid URL's to the files folder.
However, for now, since you only want people who can use the schools folder also be the SAME people that can use the files folder? Then just use the same web.config for both folders - only those people with role of "school" will thus have use and rights to the files folder.
From the information you provided, then you don't really need any code changes - only to take the security settings from the school folder web.config, and copy that to the files folder, and you should be done.
Now, it is possible that the schools folder is not secured by using IIS security and roles - but then again, that would a epic face plant and horrible design choice.

Add subdirectory for locale based URL existing ASP.NET website, resolve relative paths correctly

Existing ASP.NET (MVC and webforms hybrid) website displays translated content. The language is based on a cookie that stores the user's preference. There is no change in the URL when the user changes the setting. The content is reloaded in the preferred language. For SEO, the locale should be included in the URL ( support.google.com/webmasters/answer/182192?hl=en).
I've tried the following:
1) Use URL Rewrite Module: (http://www.iis.net/learn/extensions/url-rewrite-module/setting-http-request-headers-and-iis-server-variables)
Issues:
- All hyperlinks and redirects still point to the old URL without the locale.
- Complex outbound rules required based on the folder structure and usage (mixture of absolute paths and relative paths e.g. ../, ~/, /).
- Also need to disable static compression as per documentation
- Performance considerations due to large size of Html.
- Postback results in runtime exceptions due to issue in the relative path rewrite.
- Paths defined in script files (ajax loading etc) are a huge challenge
- Base tag does not work as expected, because the Rewrite Module seems to append ../ (http://www.iis.net/learn/extensions/url-rewrite-module/url-rewriting-for-aspnet-web-forms#Using_tilda)
2) IIS 7.5 Virtual Directory: Create Virtual Directory for each language and point it to the root. i.e. www.example.com is the root and www.example.com/fr-ca/ is a virtual directory mapped back to the root
Issues:
- Runtime exception in config file saying that the virtual directory needs to be converted to application
- Converting it to application gives 500.19 error due to duplicate entries in the web config (since the virtual directory is pointing back to the root)
- I tried moving the root to another subdirectory (i.e. have a physical directory for each language) to avoid web config conflicts, but that is resulting in some sort of "kernel" error. Also, this would mean changing the physical structure of the application, and also address routing issues
3) Using sub-domains:
I have also considered using sub-domains and hosting the application independently for each language, but this has a lot of drawbacks, including having to address scalability, single sign on, cookies, domain specific stuff like analytics etc.
So what is the least painful way to include a language sub-directory in the URL, and make all links relative to that sub-directory?
Note: The site contains a mixture of absolute paths and relative paths e.g. (../, ~/, /) sometimes used in conjunction with ResolveClientUrl, ResolveUrl
In the end, we went with option 2, with the below steps:
Create a new folder, deploy a copy of the application to the new folder. The new folder should be in a different directory from the root application.
Create a new virtual application* (not virtual directory) under the root application; 1 for each new language, pointing to the new folder. (If the need arises in the future, any of the virtual applications can point a different folder customized for that specific language)
In the new folder, remove the modules and handlers sections in the system.webServer section of the web.config file (they will be inherited from the parent web.config)
If you are using SQL session state, you will need to specify a custom Application Name in the web.config, and modify TempGetAppID stored procedure so that the Application Name is the same across all the virtual applications. See the following (http://blogs.msdn.com/b/toddca/archive/2007/01/25/sharing-asp-net-session-state-across-applications.aspx)
Hopefully, all the links are resolved on the server side using Url.Content (MVC) or ResolveUrl (webforms). If not, they need to be fixed. Any paths specified in javascript would not automatically resolve to the virtual application either (they would still be resolved to root application)
Test the heck out of it. Each and every link. (A tool like ScreamingFrog may help to make sure that no 404s are returned, methinks. But it wouldn't solve HTTP POST)
Note that depending on custom error handling, and any existing URL rewrite rules, the steps maybe different.
Summary: option 1 (URL Rewrite) is totally impractical. Option 2 (sub-directory) is the most practical solution, however it is not quite as straightforward as it should've been.

Referencing relative (parent) path with sudomain

I have a website with several subdomains that direct the user into a subfolder on my site. Inside each subfolder is a Default.aspx file which does some processing and then redirects the user to "../Default.aspx".
This works fine if you type the full URL to that page. If you try to access it through the subdomain, the ".." parent is not being parsed correctly, and just concatenates the subfolder path into the main path and I get a page not found.
The root path of my application is www.domain.com/root.
The subdomain points to subdomain.domain.com/root/subfolder
When I navigate to subdomain.domain.com, I get this error:
"404 - /root/subfolder/root/Default.aspx not found"
All I want is for subdomain.domain.com to redirect the user up one folder level to www.domain.com/root/Default.aspx
Can anyone help? Is this a feature/restriction of using a shared hosting provider - the subdomains are restricted to the folder where they are pointed?
Your description is a bit confusing, since you mix local paths and URLs together. Am I right that you are trying to do: Page at subdomain.domain.com/root/subfolder/Default.aspx redirects to www.domain.com/root/Default.aspx?
That means you want to do 2 things:
Redirect from sub-domain subdomain to sub-domain www, and
Navigate to a file one folder up.
Both things you can do in a single HTTP redirect. For this, use the Response.Redirect method, and make sure that in the URL you use the www sub-domain, and the correct absolute path to the page you want to show.
Response.Redirect("http://www.domain.com/root/Default.aspx");
Update
Or, redirect to a URL relative to the current page, in the same domain.
Response.Redirect( Page.ResolveClientUrl( "../Default.aspx" ) );
Update 2
Or, use the Host HTTP header to distinguish on subdomains and switch programatically in your shared codebase.
The answer is to point all the subdomains to the same folder (the main code base) and then in the Master file, switch based on the http header. If they are coming in from partner1.domain.com, use css1 - if they are from partner2.domain.com, use css2, etc.
This allows me to use relative paths throughout the code AND preserve the subdomain in the browser's URL bar.
One caveat - if you are testing in multiple environments (I have a DEV and a TST) you need some code to detect which environment you are in and operate a little differently, since the http header host would show something like "localhost:51510". For me, those subdomains only exist in my Production environment.
Credit to bgever - thanks!

Is it possible to create virtual subdomains?

I have a wordpress based site, that has data divided in it on the basis of country and state.
So the url goes like this
http://xyz.com/US/category_id
http://xyz.com/UK/category_id
there are no seperate folders for UK or US.
I was wondering if it possible to create multiple sub-domains like this
http://US.xyz.com
http://UK.xyz.com
without having different folders to route the sub-domains?
Of course.
Yes, but how you implement this depends on the type of service you use. Most light users purchase a shared hosting service. Your suppliers FAQs should give you advice. Some provide a control panel to map individual subdomains to separate subdirectories under your document root. Others simply map *.youdomain.com to your document root. In this latter case you need to use a .htaccess file and decode the HTTP_HOST to map each subdomain to a separate directory yourself.
If you do search here for [.htaccess] HTTP_HOST votes:2 then you will finds lots of examples of how to do this. :-)

ASP.NET - Response redirect to different folder

I am attempting to redirect a gridview on selection. However, I am getting stuck on redirection when the page I am trying to redirect to is in a different folder.
The gridview is in a folder named HR. I am trying to redirect this to a file called Staff within a folder called Staff (Staff\Staff). How can I redirect to a different folder?
If e.CommandName = "Select" Then
'Add to session variable; translate the index of clicked to Primary Key
Session.Add("DetailsKey", GridView1.DataKeys(e.CommandArgument).Value.ToString)
Response.Redirect("staff\staff.aspx")
End If
Response.Redirect("~/staff/staff.aspx")
The main thing is to use / rather than \. You aren't redirecting to a folder on the server, but to a path on the website (the fact that this means a folder on your server is just an implementation detail).
You can do all the forms that you can with relative links. Hence "staff/staff.aspx" goes to the file called staff.aspx in the folder called staff that is in the current folder (assuming your folder-and-file based system). "../staff/staff.aspx" goes up a folder, then to staff then to staff.aspx. "../../staff/staff.aspx" goes up two first. "/staff/staff.aspx" goes to the root of the domain on (http://mysite.com/staff/staff.aspx, etc).
As well as all of these, "~/staff/staff.aspx" goes to the root of the application, then to staff within that, then to staff.aspx. This is useful if you work on the site such that this would be in http://localhost/currentProject/staff/staff.aspx because the project is at http://localhost/currentProject/ but deployed to http://mysite.com/staff/staff.aspx as the site is at http://mysite.com/. This way the same code works both ways.
This should do the trick
Response.Redirect("~/staff/staff.aspx");

Resources