阻止.php .html扩展名

时间:2016-09-18 10:18:08

标签: php html .htaccess

我正在使用Godaddy域名,我想阻止/删除我的文件扩展名,例如:www.example.com/index.phpwww.example.com/index

我知道.htaccess所以这里是我的RewriteRule代码

<IfModule mod_rewrite.c>
 Options +FollowSymLinks
 RewriteEngine On
 RewriteBase /

 # Add trailing slash to url
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
 RewriteRule ^(.*)$ $1/ [R=301,L]

# Remove .php-extension from url
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteCond %{REQUEST_FILENAME}\.php -f
 RewriteRule ^([^\.]+)/$ $1.php 

# Remove .html-extension from url
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteCond %{REQUEST_FILENAME}\.php -f
 RewriteRule ^([^\.]+)/$ $1.html 

</IfModule>
有人帮帮我。

1 个答案:

答案 0 :(得分:1)

在Apache

因为您使用的是GoDaddy,所以需要在.htaccess文件中启用MultiView。

添加以下代码:

Options +MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

应该有效。参考:https://alexcican.com/post/how-to-remove-php-html-htm-extensions-with-htaccess/

在IIS / Windows

IIS使用web.config文件而不是.htaccess文件。在根目录中创建文件web.config(或者您希望应用规则的每个目录),并在其中放入以下代码

<rewrite>
  <rule name="hide .php extension" 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}.php" />
  </rule>
</rewrite>

参考:https://www.saotn.org/iis-url-rewrite-hide-php-extension/