Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URL Rewrite - Remove .html extension

So the idea is to remove the .html extension from each page like so...

www.website.com/File.html > www.website.com/File
www.website.com/Folder/File.html > www.website.com/Folder/File

Now I've managed to do this using a URL Rewrite, but it means having to write a rewrite for each page, which is time consuming, not efficient and impractical say if the website is more than 20 pages.

Is there a way to do this by writing just one or two rewrites in the web.config?

like image 590
Ryano Avatar asked Apr 18 '12 13:04

Ryano


People also ask

How do I remove .html extension from URL?

html extension can be easily removed by editing the . htaccess file.

How do I hide PHP extension in URL?

By setting expose_php to off in your php. ini file, you reduce the amount of information available to them. For this to work effectively, you must rename your PHP files with the above extensions.


1 Answers

This solution worked for me in the end:

<rule name="RedirectUserFriendlyURL1" stopProcessing="true">
    <match url="^(.*)\.(.*)$" />
    <conditions>
        <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
    </conditions>
    <action type="Redirect" url="{R:1}" appendQueryString="false" />
</rule>
<rule name="RewriteUserFriendlyURL1" stopProcessing="true">
    <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}.(.*)" />
</rule> 
like image 199
Ryano Avatar answered Oct 31 '22 14:10

Ryano