Remove .php extensions using web.config on IIS7

I have a .htaccess file that removes the .php extension from files, converting for example so.com/question.php to so.com/question, and also redirects the index file in a folder like so.com/answer/index. php redirects to so.com/answer/ exactly as described in this answer

I just installed my site locally on IIS7 and you need to recreate the same behavior in web.config , but I don’t know where to start converting / rewriting the .htaccess file.

+4
source share
1 answer

I found that IIS7 and above can import Apache.htaccess rules using the Rewrite URL module.

  • Install Rewrite URL Module through Microsoft Web Platform Installer
  • Launch IIS Manager and on the left, in the "Connections" panel, select the desired site (for example, the default website).
  • In the center (view of functions), double-click the URL Rewrite .
  • In the right pane, click Import Rules ... , then paste your rules from the .htaccess file into the Rewrite Rules box
  • In the right column, click Apply.

In the specific question above, the following .htaccess redirection rules

RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^(.*)$ $1.php [NC,L] 

generate the following web.config file.

 <?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <rewrite> <rules> <rule name="Imported Rule 1" 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> 
+22
source

Source: https://habr.com/ru/post/1434412/


All Articles