简体   繁体   中英

How to hide PHP file name and extension in url (IIS)

How to hide filename.extension in url but not in .htaccess. I'm using IIS server and I need to play with web.config

With code below I can hide only extention which is .php:

<rewrite>
  <rules>
    <rule name="Redirect .php extension" stopProcessing="false">
      <match url="^(.*).php$" ignoreCase="true" />
    <conditions logicalGrouping="MatchAny">
      <add input="{URL}" pattern="(.*).php$" ignoreCase="false" />
    </conditions>
      <action type="Redirect" url="{R:1}" redirectType="Permanent" />
    </rule>
    <rule name="hide .php extension" stopProcessing="true">
      <match url="^(.*)$" ignoreCase="true" />
    <conditions>
      <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
      <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
      <add input="{REQUEST_FILENAME}.php" matchType="IsFile" />
    </conditions>
      <action type="Rewrite" url="{R:0}.php" />
    </rule>
  </rules>
</rewrite>

First of all, if you already have an .htaccess file, you can import it using URL Rewrite section in IIS Manager.

Secondly, you cannot hide both filename and extension because filename identifies the script. Hiding extension in your example, you make an assumption it is .php , check if file with requested name and .php extension exists, and execute it. Eg http://some.site/page.php can be requested as http://some.site/page , and http://some.site/home.php can be requested as http://some.site/home .

Hiding both filename and extension will result with http://some.site/ in both cases, which make it impossible to identify which 'page.php' or 'home.php' should be called.

You can redirect everything to a single php script and analyse query string yourself though. The following rule redirects every request to index.php :

<rewrite>
    <rules>
        <rule name="front controller" stopProcessing="true">
            <match url="^(.*)$" ignoreCase="false" />
            <action type="Rewrite" url="index.php" appendQueryString="true" />
        </rule>
    </rules>
</rewrite>

You may need other rules or conditions to exclude static files, directories, etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM