隐藏/删除Windows服务器中的URL扩展名

时间:2015-02-11 22:23:32

标签: html .htaccess iis

我读到.htaccess在像godaddy这样的Windows服务器上不起作用。因此,要在URL上隐藏或删除.php和.html等扩展名,可以通过使用以下代码创建web.config文件来实现此目的:

<configuration>   
  <system.webServer>   
    <rewrite>          
      <rules>             
        <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>                
      </rules>      
    </rewrite>   
  </system.webServer>  
</configuration>

我尝试制作web.config文件并将其保存在index.php与该代码的位置,但没有发生任何事情..我用这样的测试

<ul>
  <li>
    <a  href="attackontitan-3">Attack on Titan Episode 3</a>
  </li>
</ul> with a href of "attackontitan-3"

我希望网址为http://mysite/watching/attack%20on%20titan/attackontitan-3而不是http://mysite/watching/attack%20on%20titan/attackontitan-3.php

2 个答案:

答案 0 :(得分:0)

使用重写添加扩展名,如下所示:

<rule name="rewritephp">
       <!--Removes the .aspx 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" />
       </conditions>
       <action type="Rewrite" url="{R:1}.php" />
     </rule> 

或者本教程在解释您需要对IIS执行的操作方面做得很好。

http://atlantawebsites.blogspot.com/2010/06/vanity-urls-with-godaddy-hosting-using.html

答案 1 :(得分:0)

您需要添加RewriteRedirect规则。以下内容将重定向并将page.php重写为page。如果你想为html做同样的事情,只需添加这些规则。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
   <system.webServer>
      <rewrite>
         <rules>

            <!-- Remove the existing rules just incase we already defined them -->
            <remove name="RedirectPhpExtensions" />
            <remove name="RewritePhpExtensions" />

            <!-- Add a rule to redirect x.php pages to just x -->
            <rule name="RedirectPhpExtensions" enabled="true" stopProcessing="true">
               <match url="(.*)\.php$" />
               <action type="Redirect" url="{R:1}" redirectType="Permanent" />
            </rule>

            <!-- Add a rule to rewrite the x pages to x.php behind the scene -->
            <rule name="RewritePhpExtensions" enabled="true" stopProcessing="true">
               <match url="(.*)" negate="false" />
               <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}.php" />
            </rule>

         </rules>
      </rewrite>
   </system.webServer>
</configuration>

将此web.config放在域的根目录中。

然后你需要在godaddy的网站上重启应用程序池:

  

虚拟主机&gt;管理&gt; IIS管理&gt;回收应用程序池按钮