Access image through web address without including the extension - html

I've seen this around the web before but I cannot wrap my mind around how it works. Hopefully someone can help explain this.
Lets say we have a picture at this address. http://www.website.com/image-150x150.jpg
How can I set it up so if you go to http://www.website.com/image-150x150 it shows that picture?
So the only difference is I do not have to include the extension, ie. *.jpg.
On top of all this, I want to be able to refrence it like this as well:
<img src="http://www.website.com/image-150x150" />

If you are using Apache, you need to add a .htaccess rule similiar to this:
RewriteEngine On
redirectMatch 301 ^(.*)\.jpg $1
In the case of IIS (which I haven't used at all!), consider using something like this:
<rewrite>
<rules>
<rule name="rewrite php">
<!--Removes the .php extension for all pages.-->
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" negate="true" pattern="(.*).jpg" />
</conditions>
<action type="Rewrite" url="{R:1}.jpg" />
</rule>
</rules>
</rewrite>
I'm sure that you have to change this based on your need.

You can remove the extension and reference it with the img tag and it should work. Since the encoding is of jpeg format and the img tag accepts this encoding, it should be able to convert & display the correct image.

Related

Web.Config redirect to maintenance page?

From https://gist.github.com/stevensk/c8108311b82ba1591cb1be018bbe0119, I have added the code below to our web.config for "site maintenance page redirect".
For an IP that isn't mine (mine is 3rd item down in IP list), the user is directed to the site maintenance page, but it hasn't been displaying the background image, specified fonts, or other CSS file styles.
The Firefox inspector shows the following message in red:
The resource from
“https://www.example.com/pages/maintenance/message/default.htm”
was blocked due to MIME type (“text/html”) mismatch (X-Content-Type-Options: nosniff).
How can I get the maintenance page to have the correct fonts, images and other styles applied?
<rewrite>
<!-- Maintenance rewrite map
- Add allowed IP addresses to grant access during maintenance period
- To activate, uncomment Maintenance Redirect rule
- And replace last key with current IP found at /pages/test/remote-name/
-->
<rewriteMaps>
<rewriteMap name="MaintenanceIpAddressWhitelist" defaultValue="">
<add key="192.168.0.1" value="1" />
<add key="127.0.0.1" value="1" />
<add key="198.43.70.125" value="1"/>
</rewriteMap>
</rewriteMaps>
<rules>
<rule name="HTTP/S to HTTPS Redirect" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{SERVER_PORT_SECURE}" pattern="^0$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
</rule>
<rule name="ensurewww" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{CACHE_URL}" pattern="^(.+)://(?!www)(.*)" />
</conditions>
<action type="Redirect" url="{C:1}://www.{C:2}" redirectType="Permanent" />
</rule>
<!-- Maintenance Rule -->
<rule name="Maintenance Redirect">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{MaintenanceIpAddressWhitelist:{REMOTE_ADDR}}" matchType="Pattern" pattern="1" negate="true" />
<add input="{REQUEST_URI}" pattern="^/pages/maintenance/message/default.htm$" negate="true" />
</conditions>
<action type="Redirect" url="/pages/maintenance/message/default.htm" appendQueryString="false" redirectType="Temporary"/>
</rule>
</rules>
</rewrite>
I ended up embedding images, css, svg and font files as base64 into the default.htm page.

What is Rewrite Html tag in web.config

I found this line of code in my web.config, and I cannot understand what does it exactly do for my website. What is the need of this in my web.config?
<rule name="RewriteHTML">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions> <action type="Rewrite" url="{R:1}.html" />
</rule>
You have a rule named "RewriteHTML" (that's up to you).
The match element is the pattern you want to match (regular expression). In your case, you match everything.
If you want to test Regex: https://regex101.com/
The action element tells what should be done with request that match the pattern. The type Rewrite tells that the request should be rewritten to another URL.
The conditions in this case tells to not match requests to files or directories.
So what happens is that you add ".html" to all your incoming requests.
For more clarification on {R:1} notation: IIS URL Rewrite {R:N} clarification

IIS - Remove trailing slash when directory

I want to remove trailing slashes from all requests from "/" to "", even when it's a directory. For instance:
Folder: http://bleh.local/sign-in/
File to be served: http://bleh.local/sign-in/index.html
Default document: index.html
I would like to redirect it to "http://bleh.local/sign-in"
Expected behavior: /sign-in/ redirected to /sign-in
Actual behavior: /sign-in redirected to /sign-in/
Web.config:
<rule name="RemoveTrailingSlashRule1" enabled="true" stopProcessing="true">
<match url="(.*)/$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="{R:1}" />
</rule>
applicationHost.config:
<sectionGroup name="rewrite">
<section name="globalRules" overrideModeDefault="Allow" allowDefinition="AppHostOnly" />
<section name="rules" overrideModeDefault="Allow" />
<section name="outboundRules" overrideModeDefault="Allow" />
<section name="providers" overrideModeDefault="Allow" />
<section name="rewriteMaps" overrideModeDefault="Allow" />
<section name="allowedServerVariables" overrideModeDefault="Allow" />
</sectionGroup>
With this setup, I get infinite redirects because I removed this instruction from the original setup:
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
I think my problems come from this: https://support.microsoft.com/en-ca/help/298408/iis-generates-courtesy-redirect-when-folder-without-trailing-slash-is
Any ideas?
Note: How can I configure IIS to serve directories without trailing slashes and always serve relative to root path? is not solving my problem. The backslashes are added consistently.
This is because of the <defaultDocument> in IIS.
<defaultDocument> rule is enabled in IIS by default which forces the page to be redirected to the url with trailing slash, if it did not find a file. domain/sign-in/
<rule name="RemoveTrailingSlashRule1" enabled="true" stopProcessing="true">
<match url="(.*)/$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="{R:1}" />
</rule>
Now this rule, matches the url with the trailing slash (and matches directory) and in turn redirects to the one without the trailing slash. domain/sign-in which creates a loop.
The solution is to add the rule<defaultDocument enabled="false"></defaultDocument>.
https://learn.microsoft.com/en-us/iis/configuration/system.webserver/defaultdocument/

Returning a custom response code for for serving static html when using IIS ApplicationInitialization remapManagedRequestsTo feature?

I'm currently using the ApplicationInitialization feature of IIS to warm up my ASP.NET application. I've set the attribute remapManagedRequestsTo to "warmup.html".
<applicationInitialization remapManagedRequestsTo="warmup.html" skipManagedModules="true" doAppInitAfterRestart="true" >
<add initializationPage="/home" />
<add initializationPage="/about-us" />
</applicationInitialization>
It's working well but I would like to return a custom status code when the content for Warmup.html is returned to the browser. This is so that when I run some smoke tests after deployment I get to know when the warm up has finished.
I've tried using URL Rewrite to change the status code from 200 to 555 to serve up warmup.html and it does change the status code but doesn't serve the content in warmup.html
<rewrite>
<rules>
<rule name="Change warm up status code" stopProcessing="true">
<match url="warmup.html" />
<action type="CustomResponse" statusCode="555" subStatusCode="0"/>
</rule>
</rules>
</rewrite>
Is there a way I can do both the serving of warmup.html's content AND return a custom status code of 555?
Finally found my answer in a blog post written by Morten Bock
Turns out I have to remove the two attributes remapManagedRequestsTo and skipManagedModules (default value of false) which leaves us with
<applicationInitialization doAppInitAfterRestart="true">
<add initializationPage="/home" />
<add initializationPage="/about-us" />
</applicationInitialization>
Then let the URL Rewrite module take over but we want to rewrite the response code when the Application Initialization is making the request marked by the server variable APP_WARMING_UP containing a value of 1. When this condition is met we can create a custom response as the action and pop the statusCode attribute with 555.
<rewrite>
<rules>
<rule name="WarmUp" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{APP_WARMING_UP}" pattern="1" />
</conditions>
<action type="CustomResponse" statusCode="555" statusReason="Site is warming up" statusDescription="Try again shortly" />
</rule>
</rules>
</rewrite>
Then to catch the status 555 as a custom error and direct the user to the friendly warm up page warmup.html
<system.webServer>
<httpErrors errorMode="Custom">
<error statusCode="555" path="warmup.html" responseMode="File" />
</httpErrors>
</system.webServer>

We need to force "http" for a single url . We already have a redirect of all urls to https

Our site https://www.globalcompanyformation.co.uk/ has a redirect to "https://" in the web.config file for all urls .
Now we have a form which sends information to our subdomain which is hosted in another ip address and which is not secure . This form is in our page https://www.globalcompanyformation.co.uk/Gcf-index.html . Firefox gives a warning message that the site to which you are redirected is not secure . We dont want to see this alert and when i checked whole internet the only solution was that i get the page "Gcf-index.html" to have "http" and not "https" .
Is there any way that i could force Gcf-index.html to open in "http" . The web.config coding is as follows as of now for redirecting to https for all urls .
<system.webServer>
<rewrite>
<rules>
<clear />
<rule name="Redirect to https" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
<add input="{REQUEST_FILENAME}" negate="true" pattern="^Gcf-index.*" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
</rule>
</rules>
</rewrite>
</system.webServer>
Change the second condition to
<add input="{REQUEST_FILENAME}" negate="true" pattern="Gcf-index\.html" />
Add the following rule to force http on that form page.
<rule name="Force http on GCF" stopProcessing="true">
<match url="Gcf-index\.html$" />
<conditions>
<add input="{HTTPS}" pattern="on" ignoreCase="true" />
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}{REQUEST_URI}"
redirectType="Permanent" appendQueryString="false" />
</rule>