我只需要重写一个名为gallery
的目录的url。
我想要 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]
最佳答案
通过 httpd.conf
启用 mod_rewrite 和 .htaccess,然后将此代码放入 DOCUMENT_ROOT
目录下的 .htaccess
中:
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]
关于.htaccess - 仅针对一个目录删除带有 .htaccess 的 .php 扩展名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14833805/