Last updated: January 28th, 2024
After talking to an SEO specialist, it was recommended that one of my websites improve their URLs. As he put it: “Google is overrewarding domains that match the order of search terms these days, and that includes subdomains.“ So a URL that was https://whatever.com/products/category should instead be: https://categorty.whatever.com perhaps because sub domains are technically seen by Google as separate sites this can increase ranking.
Enabling this required a bit of configuration. A new route needed to added to my Global.asax file that would find sub domains, parse out the sub domain string and then redirect them to the correct action and controller. Cookies that work with the shopping cart would need to be able to work across all sub domains so shoppers could see their cart throughout their session. Links all over the site that were application relative would need to be able to parse out the domain and ignore the sub domain otherwise the link would be relative to the sub domain, this is important because duplicate content causes confusion for Google Bot. IIS would need to be configured to direct all sub domain to the root application to enable the application to process the sub domain request.
To test this required some set up as well. I would not be able to use the IIS Express server that dynamically generates a port, I would need to use my local IIS and a create version of the site that I could debug with. My computer's host file would need to be manually edited for each sub domain that I wanted to test this with. Finally, I decided also to put in conditional statements that would check to see if it was localhost or not to have links I could work with locally.
This route was then added to the RegisterRoutes event:
routes.Add(new ExampleRoute());
Now my controller's action could get a parameter passed to it which would be the sub domain. I decided to do this so that I did not need to modify the action or view, it just took in the parameter from a different source through the route. There are many other ways to do this but I wanted to use one in which I could pass a value to a controller so that I could avoid any hard coding or redundant route populating.
To test this locally I needed to install all the ASP.NET features on my local computer such as: World Wide Web Services > Application Development Features > ASP.NET from the Programs and Features menu as well as other. I had to set up a web site on my local IIS that would resolve to a path without a port and with the NETWORK SERVICE account having at least Modify permission on the folder that had the web files. I would then need to run Visual Studio as an Administrator to use the site at this path. In my project's settings I need to right click the web project and select: Use Local IIS Web Server but un-check Use IIS Express. My Project Url would need to not have a port number and a Virtual Directory would need to be created.
From here I needed to run a command line as an Administrator then open the host file:
notepad c:\windows\system32\drivers\etc\hosts
At the last point I decided to add two local IP host pairs to test with:
127.0.0.1 localhost
127.0.0.1 category.localhost
This allowed me to test with only one of my sub domains for proof of concept. Then I had to File > Save As, All Files then overwrite the hosts file with my saved one. Now I was set to test this locally.
Lastly, I had to go through my various menu, product, category, department and other links to parse out the domain (or localhost) and make that the base from which to be relative. With regards to the shopping cart cookie, I needed to set the cookie's Domain to start with “.” as in ".mouthguardstore.com" so that all sub domains, and even non sub domains, would be able to access the cookie. The final result was that I could now deploy to my shared hosting, which had been set to work with sub domains, and have the URLs resolve as I expected.
Comments
No Comments