删除文件扩展名htaccess无法正常工作

时间:2014-03-01 10:56:01

标签: php .htaccess mod-rewrite url-rewriting

以下代码无法删除文件扩展名.php

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]

我的网址未从example/about.php更改为example.com/about

4 个答案:

答案 0 :(得分:2)

我找到了答案。它的工作。代码如下重写和重定向url

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_FILENAME}.php [L]

它重写并重定向URL。谢谢大家的回答。

答案 1 :(得分:1)

此RewriteRule应从请求中删除.php扩展名。尝试将它放在RewriteConds之前。

RewriteRule ^(.+)\.php$ /$1 [R]

答案 2 :(得分:1)

试试这个

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*)$ $1.php

答案 3 :(得分:0)

试试这个

RewriteEngine On

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ $1\.php

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*[^/])/?$ $1.php [L]