使用.htaccess删除.php

时间:2016-04-28 20:42:23

标签: php apache .htaccess

我正在我的网站var/www/mysite/的路径中创建.htaccess文件,以删除我的文件" .php",但无法正常工作

# Apache Rewrite Rules
 <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 

# End of Apache Rewrite Rules
</IfModule>

这是我第一次这样做,如果我做得不好,或者这是否在正确的地方,那么我来找你。

1 个答案:

答案 0 :(得分:0)

在站点根目录中尝试此代码.htaccess:

Options +FollowSymLinks
RewriteEngine On

# Add trailing slash to url that is not a file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L,R=302,NE]

# Add .php extension from urls internally
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php