So I have a site.com/blog/12/12/articleNames and i want to redirect everything to site.com/articles/12/12/articleNames instead.
so in my blog folder i have this in my web.config
<rewrite>
<rules>
<rule name="articleMove" stopProcessing="true">
<match url="^blog/([0-9]+)/([0-9]+)/([_0-9a-z-])/" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" />
</conditions>
<action type="Rewrite" url="/articles/{R:1}/{R:2}/{R:3}" />
</rule>
</rules>
</rewrite>
However, i still keep getting the 404 page.
Use IIS Failed Request Tracing to further investigate what is happening "behind the scenes".
<rule name="articleMove" stopProcessing="true">
<match url="^([0-9]+)/([0-9]+)/([_0-9a-z-]+)" />
<action type="Redirect" url="/articles/{R:1}/{R:2}/{R:3}" />
<conditions>
</conditions>
</rule>
So if the URL is http://site.com/blog/whatevergoeshere. Ignore the blog part. and each split you just do {R:1} etc.. so if the url was http://site.com/blog/12/10/the-article-name and you want it to be http://site.com/articles/12/10/the-article-name is what the above does.
Related
I had a page http://domain1.com/blog.aspx.
This page I have deleted and created a website with a new domain just for this page.
like below http://domain2.com/blog.aspx
Then I have added a rule in domain1.com web.config like below
<rewrite>
<rules>
<rule name="Redirect blog" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^domain1.com/blog.aspx$" />
</conditions>
<action type="Redirect" url="http://domain2.com/blog.aspx" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
But I get 404 error while visiting http://domain1.com/blogpage.aspx
How can I fix this issue?
A few issues...
{HTTP_HOST} = The host name which is domain1.com, so it will never match your pattern
Also you can do this pattern="^domain1.com/blog.aspx$" the . need to be escaped.
Now you have to match also on the request_uri to capture the page. Below should work. Providing you are re-directing domain1.com/blogpage.aspx to domain2.com/blog.aspx
<rule name="Redirect blog" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll" >
<add input="{HTTP_HOST}" pattern="^domain1\.com$" />
<add input="{REQUEST_URI}" pattern="blogpage\.aspx$" />
</conditions>
<action type="Redirect" url="http://domain2.com/blog.aspx" redirectType="Permanent" />
</rule>
</rules>
I am having this problem with my site getting redirect to www.www.sitename.com.
I have this code written in web.config which working fine for my other website.
<system.webServer>
<rewrite>
<rules>
<clear />
<rule name="Redirect to WWW" enabled="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^www\.([.a-zA-Z0-9]+)$" />
</conditions>
<action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
This is 100% wokring code other sites but having problem of www.www.sitename.com on this site srilanka-tours.co
How about doing it less generic and more focused on your actual host name?
E.g.:
<rule name="Redirect to WWW">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.srilanka-tours\.co$" negate="true" />
</conditions>
<action type="Redirect"
url="http://www.srilanka-tours.co/{R:1}"
redirectType="Permanent" />
</rule>
(This is an excerpt from a real-world rewrite rule).
The above rule is similar to what you get when using the "Canonical Domain Name" in the IIS Managers' "wizard" for creating rewrite rules. See the article on Scott Guthrie's blog.
How would I modify this rule to include non-www to www redirect?
<rule name="Force Https" stopProcessing="true">
<match url="healthcheck.html" negate="true" />
<conditions>
<add input="{HTTP_X_FORWARDED_PROTO}" pattern="https" negate="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
</rule>
Is there a better way to force HTTPS sitewide? I am using ASP.NET MVC 5 on IIS 8.5
I think you just need to add another rule where it checks if the HTTP_HOST variable just contains your host without the www. prefix, and redirect if so:
<!-- You first rule. Note stopProcessing is now false. -->
<rule name="Force Https" stopProcessing="false">
<match url="healthcheck.html" negate="true" />
<conditions>
<add input="{HTTP_X_FORWARDED_PROTO}" pattern="https" negate="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
</rule>
<!-- Additional rule. -->
<rule name="Force WWW" stopProcessing="true">
<match url="healthcheck.html" negate="true" />
<conditions>
<add input="{HTTP_HOST}" pattern="^yourdomain\.com"/>
</conditions>
<action type="Redirect" redirectType="Permanent" url="https://www.{HTTP_HOST}{REQUEST_URI}" />
</rule>
Just change yourdomain.com above to your actual host domain name.
To answer your other question, I think URL redirect (through URL Rewrite) is the simplest way to force HTTPS without returning a 403 to your users who still try to access your site via HTTP.
UPDATE
In response to your comment regarding the double 301, you could try this single rule. I do not have my laptop at home to verify, but I think this will work:
<!-- This rule will capture any http request regardless of the 'www.' prefix in the URL -->
<rule name="Force Https" stopProcessing="true">
<match url="healthcheck.html" negate="true" />
<conditions>
<add input="{HTTP_X_FORWARDED_PROTO}" pattern="^http$" />
</conditions>
<action type="Redirect" url="https://www.yourdomain.com{REQUEST_URI}" redirectType="Permanent" />
</rule>
<!-- This rule will capture https request that does not have the 'www.' prefix in the URL -->
<rule name="Force WWW Prefix" stopProcessing="true">
<match url="healthcheck.html" negate="true" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_X_FORWARDED_PROTO}" pattern="https" />
<add input="{HTTP_HOST}" pattern="^yourdomain\.com$"/>
</conditions>
<action type="Redirect" url="https://www.yourdomain.com{REQUEST_URI}" redirectType="Permanent" />
</rule>
I'm trying to to redirect on xyz.sample.com to www.sample.com/order/xyz, which works fine, but the problem is now that resources like xyz.sample.com/js/file.js redirects to www.sample.com/order/xyz/js/file.js which is no right, how can i only do the redirection when the url is just xyz.sample.com and im not trying to access anything inside, below is my rewrite rule:
<rewrite>
<rules>
<rule name="Rewrite service provider to web ordering page" enabled="true" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(.*)\.sample\.com$" ignoreCase="true" />
<add input="{HTTP_HOST}" negate="true" pattern="^manage\.sample\.com$" ignoreCase="true" />
</conditions>
<action type="Rewrite" url="order/{C:1}" />
</rule>
</rules>
</rewrite>
Thanks.
I'm using ASP .NET rewriteModule to rewrite http://example.com to http://www.example.com.
<section name="rewriteModule" type="RewriteModule.RewriteModuleSectionHandler, RewriteModule"/>
Then i have this inside <system.webServer>.
<rewrite>
<rules>
<rule name="Canonical" stopProcessing="true">
<match url=".*"/>
<conditions>
<add input="{HTTP_HOST}" pattern="^([a-z]+[.]com)$"/>
</conditions>
<action type="Redirect" url="http://www.{C:0}/{R:0}" redirectType="Permanent"/>
</rule>
</rules>
</rewrite>
Now i want to remove all the .aspx in the end of my pages. Example:
http://www.example.com/Register.aspx
Will turn into:
http://www.example.com/Register/
How can i do that?
I'm on Shared Web Hosting on GoDaddy using IIS7.
These are the standard rewrite rules I start every project with. I use only clean URLs for all the pages (example first rule works for www.example.com/about and second rule www.example.com/product/123)
<rewrite>
<rules>
<rule name="Rewrite default to aspx" stopProcessing="true">
<match url="^$" ignoreCase="false" />
<action type="Rewrite" url="default.aspx" />
</rule>
<rule name="Rewrite page to aspx" stopProcessing="true">
<match url="^([a-z0-9/]+)$" ignoreCase="false" />
<action type="Rewrite" url="{R:1}.aspx" />
</rule>
</rules>
</rewrite>
Pages where I need to parse out the ID (this case number only) and add it to the query string I add a similar rule to the front:
<rule name="Rewrite Product ID" stopProcessing="true">
<match url="^product/([0-9]+)$" ignoreCase="false"/>
<action type="Rewrite" url="product.aspx?id={R:1}"/>
</rule>
If you want to use lower and upper case letters in the URL, set ignoreCase="true"
Edit to answer your second question plus a bonus
This rule will redirect aspx page to the clean URL:
<rule name="Redirect to clean URL" stopProcessing="true">
<match url="^([a-z0-9/]+).aspx$" ignoreCase="true"/>
<action type="Redirect" url="{R:1}"/>
</rule>
Replace url="{R:1}" with url="{ToLower:{R:1}}" to change URL to lowercase. See below why you would want to do this.
Also a good idea to update the Form action so that post backs don't return back to the ugly URL. Using IIS 7.5 or newer this should work:
if (!String.IsNullOrEmpty(Request.RawUrl))
form1.Action = Request.RawUrl;
or for IIS 7:
if (!String.IsNullOrEmpty(Context.Request.ServerVariables["HTTP_X_ORIGINAL_URL"]))
form1.Action = Context.Request.ServerVariables["HTTP_X_ORIGINAL_URL"];
One more thing to keep in mind... it's a good idea to keep all URLs lower case. Mixing lower/upper case characters in the URL creates duplicate content issues for SEO/Google. For example website.com/About and website.com/about will load the same page, but Google will index them as two separate pages.
First you need to remove the .aspx (default.aspx) and redirect to default to change the browser address then add the .aspx and rewire to page using IIS
<rewrite>
<rules>
<clear />
<rule name="Redirect to clean URL" enabled="true" stopProcessing="true">
<match url="^([a-z0-9/]+).aspx$" ignoreCase="true" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Redirect" url="{R:1}" />
</rule>
<rule name="RewriteASPX" enabled="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="{R:1}.aspx" />
</rule>
</rules>
</rewrite>
<rewrite>
<rules>
<remove name="RewriteUserFriendlyURL1" />
<remove name="RedirectUserFriendlyURL1" />
<rule name="RedirectUserFriendlyURL2" stopProcessing="true">
<match url="^www\.myserver\.com/(.*)\.aspx$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
</conditions>
<action type="Redirect" url="www.myserver.com/{R:1}" appendQueryString="false" />
</rule>
<rule name="RewriteUserFriendlyURL2" stopProcessing="true">
<match url="^www\.myserver\.com/(.*)$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="www.myserver.com/{R:1}.aspx" />
</rule>
</rules>
<outboundRules>
<remove name="OutboundRewriteUserFriendlyURL1" />
<rule name="OutboundRewriteUserFriendlyURL2" preCondition="ResponseIsHtml1">
<match filterByTags="A, Form, Img" pattern="^(.*)www\.myserver\.com/(.*)\.aspx$" />
<action type="Rewrite" value="www.myserver.com/{R:1}" />
</rule>
</outboundRules>
</rewrite>
this will do it - I have generated this vis IIS on my local machine - change myserver.com to your own URL. you can change the regex to actually take care of the x.aspx part of the url then it should work across all pages