Redirect .htm to .html - IIS - iis

We've rebuilt our Website and our extensions will be .html. Currently, they are .htm. When a user visits our site from a bookmarked page for example that's .htm, I want it to redirect to the .html version.
I have looked through stackoverflow to see if there is already a Q&A on this. Most are Apache - .htaccess related. I'm using IIS. The closest answer I could find on stackoverflow is this one: IIS url rewrite rewriting all .asp to .html
I tried following the advice in that post, but it's not working. The webpage tells me This webpage has a redirect loop. and Error code: ERR_TOO_MANY_REDIRECTS
I'm in the URL Rewrite module in IIS. I create a new "blank rule" for inbound rules. Here are the settings I'm using as advised by that stackoverflow post: (BTW, I would have posted this in that thread, but it's over 3 years old, so I wasn't sure if that was advised)
Requested URL: Matches the Pattern
Using: Regular Expressions
Pattern: (.*).htm(.*)
Ignore Case is checked.
Conditions: Local Grouping: Match All
I Added a condition...
Condition Method: {REQUEST_METHOD}
Check if input string: Matches the Pattern
Pattern: ^GET$
Ignore Case is checked
Action type: Redirect
Redirect URL: {R:1}.html{R:2}
Append Query String is checked
Redirect type: Permanent (301)
I'm doing a 301 because I want the search engines to know our URL extensions have changed. Our URLs are exactly the same. The only difference is they're going to be .html instead of .htm. So... something/something.htm is something/something.html.

The rule should be as simple as:
<rule name="htm to html" stopProcessing="true">
<match url="(.+).htm$" />
<action type="Redirect" url="{R:1}.html" />
</rule>
Or in the configuration screen:
This rule will redirect any url ending with .htm and containing at least one more character than .htm to the same url but replacing .htm with .html

Related

Azure CDN with Rewrite Rules

I have an Azure CDN with a website origin for my domain and it picked www.mydomain.co.uk automatically (with no option for me to change it). I'd rather not use a Storage origin as I have automatic deployment from Github. So, azxxxxxxx.vo.msecnd.net will retrieve content from www.mydomain.co.uk and cache it locally.
However, I have a URL write rule which will redirect www.mydomain.co.uk to mydomain.com (for various SEO reasons). This will cause requests for CDN content to return a 301 redirect, rather than serving up content to the CDN for it to cache. Is there some way to stop the URL rewrite when content is requested by the Azure CDN?
You're more than welcome to see the code and URL rewrite rules at https://github.com/brentnewbury/PersonalSite/blob/master/web.config
For others who might want to know how I solved the issue, I decided to write a URL rewrite rule (see below) that looks for /cdn/ and route that through and no longer process other rules. This seems to work nicely. It means references in my HTML will look like //azxxxxxxx.vo.msecnd.net/cdn/img/pic.jpg for a image that is stored in /img/pic.jpg.
<rule name="CDN Passthrough" stopProcessing="true">
<match url="(.*)(cdn/)([\S]+)" />
<action type="Rewrite" url="{R:1}/{R:3}"/>
</rule>

Redirect anything not already redirected with 301 on IIS7

We recently setup a new website on a new domain using WordPress, to replace our old website using flat files. Because of time constraints, we had little time to plan and ended up having someone setup the redirects in IIS Manager.
This means that almost every folder in the old website has something like the following for each page:
<location path="old-file.html">
<system.webServer>
<httpRedirect enabled="true" destination="http://new-domain.com/new-page/" exactDestination="true" childOnly="true" httpResponseStatus="Permanent" />
</system.webServer>
</location>
The problem is that this was a massive site with ~15k pages and only about 1k pages were manually redirected to new URIs. This means we have potentially 14k pages of legacy content (effectively dead URLs, some of it is so old) but we need to it redirect to the root of the new domain.
So almost every folder has a web.config with one instance of the codeblock above for each page redirected, but we need every page that wasn't redirected in this manner to redirect to a fixed location.
Manually configuring isn't an option because of the number of pages. I thought about using a tool to add a line of PHP to handle the redirect to every .php and .html file (PHP had to parse html files on this site for legacy content) but it's far from ideal.
Ideally, there'd be something I could put in the web.config which would say "For any request not already redirected via <location>, redirect it to "http:// domain.com". Even if this meant appending something to the web.config already in each folder, it would save days of work, say if no locations match, then redirect.
I've searched for having multiple locations or wildcards in <location> blocks but doesn't seem possible so I'm not sure what do do here—any help appreciated!
Suggestion: why not you redirect your 404 page to home page of any other you wish to.. if someone lands to any old page that no longer exists should go to 404 so redirect 404 to homepage, common practice in case of ecommerce sites they redirect their discontinued product page to search page. If that helps you can google "redirect 404 to homepage wordpress" there are lots of plugins that would do it, but as IIS you can simply config your web.config.
If the urls are now dead and you dont have replacement content to redirect them to, then the proper response would be to return a 404/410. Redirecting dead urls in bulk to the homepage will be seen by Google as a soft 404. https://support.google.com/webmasters/answer/181708?hl=en

.aspx pages redirected but not .html

We switched our website from a custom .NET solution to a Wordpress site. As a result, I have needed to write a couple specific URL Rewrite rules in IIS to 301 redirect old urls to their equivalent pages in the Wordpress site. Considering the following 2 rules, rule 1 will match and redirect properly. However, rule 2 won't. My question is, why doesn't the rule 2 work? The only difference seems to be .html vice .aspx.
Rule 1
Pattern:
our-company/our-staff.aspx
URL:
http://www.ourdomain.com/our-company/our-staff.aspx
Rule 2
Pattern
labs/some-file.html
URL:
http://www.ourdomain.com/labs/some-file.html

Redirect from .php URL for only one specific file

I've just removed the .php extensions from my site and I want to redirect the old URLs to the new ones. In particular it's just for the picture.php file and none others. Pages are identical for both domain.com/picture/$1 and domain.com/picture.php/$1 and both resolve just fine, but I need to redirect all traffic from the .php extension to the non-php version.
I tried using this code which seems to work as far as showing the new correct non-.php URL in my address bar, but it breaks the site and shows a 404 for all the /picture pages.
RedirectMatch 301 ^/picture.php/(.*)$ http://domain.com/picture/$1
Do I perhaps have an error in the redirect rule? I've done some reading but I'm still new to writing rules. Is there any better method of doing this?
Thanks in advanced!
-Justin
Here's how:
<?php
header( 'Location: http://www.yoursite.com/new_page.html' ) ;
?>
Be sure that you do not have any text sent to the browser before this, or it will not work. Your safest bet is to simply remove all content from the page but the redirect code.

mod_rewrite in .htaccess to replace .aspx

I have converted a website from ASP to php...and the urls no longer require an extension. I want to strip off ".aspx" from the end of my incoming strings (from search engines for example) and then 302 redirect them to the correct page without that extension so that the rest of my mod_rewrite rules can then take over. How would I do this?
OLD URL: www.mysite.com/test/page/here.aspx
NEW URL: www.mysite.com/test/page/here/
Thanks!
Place this rule into your .htaccess file in root folder (before catch-all rewrite rule, if you have such):
RewriteRule ^(.+)\.aspx$ http://www.example.com/$1/ [QSA,NC,R=301,L]
This will redirect http://www.example.com/test/page/here.aspx to http://www.example.com/test/page/here/.
Please note, it is better (at least from SEO/browser point of view) to have 301 redirect code (Permanent Redirect) instead of 302 (Found/Temporal Redirect). Unless you may consider changing website back to aspx.

Resources