can anybody please tell me
how rewrite .php files to .html files in a folder.
or how to rewrite urls like http://examle.com/files/ (actual path )
using web.config file.

Dani AI

Generated

Short answer: there are two common ways to handle that example URL (long → short) with IIS/web.config — either redirect the deep path to a short canonical URL, or internally rewrite a short URL to the archived file. ’s extensionless-PHP approach is already a good start; below are safe, practical web.config patterns that complement that work and avoid the htaccess approach suggested (which won’t apply on Windows hosting as noted).

A. Redirect long archive URL to a short canonical URL (external 301)
This matches requests like /myfiles/2014/03/my2014.html and issues a permanent redirect to /my2014.html. The rule checks that the short file exists before redirecting to avoid broken redirects.

<rule name="ArchiveLongToShort" stopProcessing="true">
  <match url="^myfiles/([0-9]{4})/([0-9]{2})/([^/]+)\.html$" />
  <conditions logicalGrouping="MatchAll">
    <add input="{APPL_PHYSICAL_PATH}{R:3}.html" matchType="IsFile" />
  </conditions>
  <action type="Redirect" url="/{R:3}.html" redirectType="Permanent" appendQueryString="false" />
</rule>

This uses a Redirect action (301) and an IsFile check against the application physical path. (learn.microsoft.com)

B. Serve a short URL by rewriting it to a specific nested file (internal rewrite)
If the short filename must map to a specific archived path (and you have many such mappings), use a rewriteMap to keep mappings maintainable:

<rewriteMaps>
  <rewriteMap name="ShortToArchived" defaultValue="">
    <add key="my2014.html" value="/myfiles/2014/03/my2014.html" />
    <!-- add more entries -->
  </rewriteMap>
</rewriteMaps>

<rule name="ShortToArchivedRewrite" stopProcessing="true">
  <match url="^([^/]+\.html)$" />
  <conditions>
    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
    <add input="{ShortToArchived:{R:1}}" pattern="(.+)" />
  </conditions>
  <action type="Rewrite" url="{ShortToArchived:{R:1}}" appendQueryString="false" />
</rule>

Rewrite maps are ideal when you have many static mappings to maintain in one place. (learn.microsoft.com)

Troubleshooting / notes

  • Place redirect rules before rewrites and use stopProcessing="true" to prevent loops.
  • Test patterns in IIS Manager (URL Rewrite feature) and enable Failed Request Tracing if behavior is unexpected.
  • For dynamic or numerous date-derived paths, consider generating the map programmatically or using a custom provider. (learn.microsoft.com)

This complements ’s earlier solution and gives both the SEO-friendly redirect (long→short) and the maintainable internal-rewrite approach (short→nested file) depending on which behavior is required.

Recommended Answers

All 11 Replies

You can try this:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Serve PHP files sans ext" stopProcessing="true">
                    <match url="^(.*)$" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
                        <add input="{REQUEST_FILENAME}.php" matchType="IsFile" ignoreCase="false" />
                    </conditions>
                    <action type="Rewrite" url="{R:1}.php" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>
Member Avatar for Member #120589

for this use apache rewrite. there are many tutorials online

^ what he said

yeah it is working . thank you so much sir.... u given complete code . thank you so much again ...

@diafol ya there are many in online. but i like this website most. programming experts were here . this is a small google search. thank you http://www.daniweb.com


sir i was tried your example
which rewrites
ok fine

i added the following line for directory (like this ) but am not getting can you help me
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />

 <configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Serve PHP files sans ext" stopProcessing="true">
<match url="^(.*)$" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
<add input="{REQUEST_FILENAME}.php" matchType="IsFile" ignoreCase="false" />

<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="{R:1}.php" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

htaccess is the best way to ago. Try: AddType application/x-httpd-php .html in your htaccess file.

sir , htaccess is not supporting my server thats why using web.config (windows hosting)

Member Avatar for Member #120589

@raj. i apologize if my post was a little curt but i'm on bacation and im using a mobile so i was just trying to point you in the right direction. complete code solutions aren't really what this site is about though. we did not know that you were on iis so its a good thing that i didnt waste 10 minutes of my time typing out a htacces solution.

ok sir,
can you please tell me .......
how to rewrite urls using web.config (only)
for example

to
......
i was tried using google but i am not getting please help..... bigup for reply......

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.