仅使用.htaccess删除.php扩展名为一个目录?

时间:2013-02-12 13:35:14

标签: .htaccess url-rewriting

我需要只重写一个名为gallery的目录。 我希望abc.com/photos/gallery/picture/代替abc.com/photos/gallery/picture.php

.htaccess
index.php
/photos
    /gallery 
        index.php
        picture.php
        functions.php
/videos

以下是.htaccess的代码,但不起作用。

RewriteEngine on
RewriteRule ^/[.*]/?$ photos/gallery/$1.php#{QUERY_STRING} [NC,L]

1 个答案:

答案 0 :(得分:2)

通过httpd.conf启用mod_rewrite和.htaccess,然后将此代码放在.htaccess目录下的DOCUMENT_ROOT中:

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

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

## To internally forward /gallery/foo to /gallery/foo.php
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(gallery/.*?)/?$ $1.php [L,NC]