Quantcast
Channel: All Forums
Viewing all articles
Browse latest Browse all 28058

One redirect to rule them all.

$
0
0

After trying to help a fellow developer on these forums: Link here 

I began to search for a better way to do my redirects. And thus I ended up with this article: Seomoz

I took a lot of these ideas and began to impliment my own version of it. Everything seamed to work just okay, but I hit a roadbump and I hope you can help.

Okay so, a quick explanation on my rules.

My idea is to test the url, and if I find something wrong with it, I rewrite the url and let it go on, rather than redirect it instantly. If I at any point rewrite the url I set a custom server variable "Redirect" to true. Then in the end, I test if my custom server variable is true and redirect the user.

The point of it is to only have one 301 redirect, rather than a chain of redirects.

This is my rules (Sorry for the wall):

<rules><rule name="WhiteList - resources" stopProcessing="true"><match url="^resources/" /><conditions logicalGrouping="MatchAll" trackAllCaptures="false" /><action type="None" /></rule><rule name="Redirect subdomains with www to non-www" stopProcessing="false"><match url="(.*)" /><conditions logicalGrouping="MatchAll" trackAllCaptures="false"><add input="{HTTP_HOST}" pattern=".*localhost.*" negate="true" /><add input="{HTTP_HOST}" pattern="^www\.(.*)\.([^\.]+)\.([^\.]+)$" /></conditions><action type="Rewrite" url="http://{C:1}.{C:2}.{C:3}{HTTP_URL}" /><serverVariables><set name="Redirect" value="true" /></serverVariables></rule><rule name="Redirect top domains with non-www to www" stopProcessing="false"><match url="(.*)" /><conditions logicalGrouping="MatchAll" trackAllCaptures="false"><add input="{HTTP_HOST}" pattern=".*localhost.*" negate="true" /><add input="{HTTP_HOST}" pattern="^([^\.]+)\.([^\.]+)$" /></conditions><action type="Rewrite" url="http://www.{HTTP_HOST}{HTTP_URL}" /><serverVariables><set name="Redirect" value="true" /></serverVariables></rule><rule name="SEO - Remove trailing slash" stopProcessing="false"><match url="(.*)/$" /><conditions><add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /><add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /></conditions><action type="Rewrite" url="{R:1}" /><serverVariables><set name="Redirect" value="true" /></serverVariables></rule><rule name="SEO - ToLower" stopProcessing="false"><match url="(.*)" ignoreCase="false" /><conditions logicalGrouping="MatchAll" trackAllCaptures="false"><add input="{URL}" pattern="[A-Z]" ignoreCase="false" /><add input="{URL}" pattern="^.*?\.(axd|css|js|jpg|jpeg|png|gif|ashx|asmx|svc).*?$" negate="true" /><add input="{URL}" pattern="^.*/(webshop)/.*$" negate="true" /></conditions><action type="Rewrite" url="{ToLower:{R:1}}" /><serverVariables><set name="Redirect" value="true" /></serverVariables></rule><rule name="SEO - remove default.aspx" stopProcessing="false"><match url="(.*?)/?default\.aspx$" /><action type="Rewrite" url="{R:1}" /><serverVariables><set name="Redirect" value="true" /></serverVariables></rule><rule name="SEO - Trim aspx" stopProcessing="false"><match url="(.*)\.aspx$" /><action type="Rewrite" url="{R:1}" /><serverVariables><set name="Redirect" value="true" /></serverVariables></rule><rule name="SEO - non-canonical redirect" stopProcessing="true"><match url="^(.*)" /><conditions><add input="{Redirect}" pattern="true" /></conditions><action type="Redirect" url="{R:1}" /><serverVariables><set name="Redirect" value="false" /></serverVariables></rule></rules>



Now most of it actually works pretty well, but I seam to have some issues with a naked domain.

If I take a subdomain with www (Which should be redirected to non-www):
http://www.test.mysite.com

Then it appears to fail. Funny enough, if I go to a subpage, it works fine:
http://www.test.mysite.com/shop

I've traced it down to the server variable {HTTP_URL} because if I remove it, it doesn't fail, however it ofcourse doesn't do what it is supposed to either (It always redirects to the naked domain). I've tried with various variables: {URL}, {REQUEST_URI} and they all seam to end up with the same error, which is getting a bit annoying.

I really hope someone can help me figure out the bug that is clearly there, it would be greatly appriciated.

Should anyone have some improvement for the rules, feel free to respond aswell, I love working with them and I would like to make the near perfect redirects, so any suggestions are welcome.


Viewing all articles
Browse latest Browse all 28058

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>