block .php .html extension



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

我知道。htaccess所以这里是我的rewriterrule代码

<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>

谁来帮帮我

Apache

因为你使用的是GoDaddy,你需要在你的。htaccess文件中启用MultiViews。

添加以下代码:

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 h1> IS使用web。配置文件,而不是.htaccess文件。创建一个文件web。在根目录(或者您希望应用规则的每个目录)中配置,并将以下代码放入其中
<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/

最新更新