I've seen countless websites that use strange in-site links when linking back to their index pages. I've also seen problems when somebody else posts a link back to your website on a social networking website that is different than how you usually link. This can hurt your search engine optimization (SEO) on your website because the web crawlers will consider each variation a distinct webpage. Here are a few variations of the home page of NealGrosskopf.com.
Home Page Possibilities
- http://www.nealgrosskopf.com/
- http://www.nealgrosskopf.com/default.asp
- http://nealgrosskopf.com/
- http://nealgrosskopf.com/default.asp
It's important to standardize a way for linking to index pages within folders on your website. It's also good to relay this onto co-worker who may also be working on the same website as yourself. Although web workers try their best to be consistent with their inner-site linking, it's impossible to maintain in a multi-user environment. Because of this I created a way to do 301 redirects for home pages.
Setup: Variables
'301 Redirect - URL Must Contain www.domain.com
CorrectDomain = "www.domain.com"
DefaultPage = "/index.asp"
QueryStrings = request.ServerVariables("QUERY_STRING")
URL = request.ServerVariables("URL")
While there is of course a shorter way to do this, I decided to create variables for this article since I feel they are easier to understand.
301 Domain Check
if instr(request.ServerVariables("SERVER_NAME"),CorrectDomain) = 0 then
end if
First we need an IF statement to check the domain name against our CorrectDomain variable. If the current domain does not contain the domain we want it to, we enter the IF statement.
Query Strings
if QueryStrings <> "" then
QueryStrings = "?" & QueryStrings
end if
Next we check if there are any query strings and if so, add the question mark to the beginning.
Default/Home/Index Page
if instr(URL,DefaultPage) then
URL = replace(URL,DefaultPage,"/")
end if
Then we check the path to the page and remove default,index or home.asp from the string to get a clean trailing forward slash.
301 Redirect
response.Status = "301 Moved Permanently"
response.AddHeader "Location","http://" & CorrectDomain & URL & QueryStrings
response.End
Finally we do the actual 301 redirect in classic ASP using all the variables we built.
Putting It All Together
'301 Redirect - URL Must Contain www.domain.com
CorrectDomain = "www.domain.com"
DefaultPage = "/index.asp"
QueryStrings = request.ServerVariables("QUERY_STRING")
URL = request.ServerVariables("URL")
if instr(request.ServerVariables("SERVER_NAME"),CorrectDomain) = 0 then
if QueryStrings <> "" then
QueryStrings = "?" & QueryStrings
end if
if instr(URL,DefaultPage) then
URL = replace(URL,DefaultPage,"/")
end if
response.Status = "301 Moved Permanently"
response.AddHeader "Location","http://" & CorrectDomain & URL & QueryStrings
response.End
end if