2013-02-12 126 views
2

我需要重寫url只有一個名爲gallery的目錄。 我想有abc.com/photos/gallery/picture/代替abc.com/photos/gallery/picture.php只爲一個目錄刪除.php擴展名.htaccess?

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

這裏是我的代碼.htaccess,但它不工作。

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

回答

2

啓用mod_rewrite的,並通過httpd.conf的.htaccess,然後把這個代碼在你.htaccessDOCUMENT_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] 
+0

噢,它的偉大工程。謝謝@anubhava!除了我不得不將'gallery /'改成'photos/gallery /' – Steffi 2013-02-12 14:10:48

+1

@Steffi:不客氣,我忽略了它是'/ photos/gallery'而不是'/ gallery' – anubhava 2013-02-12 14:21:51