.htaccess - 仅针对一个目录删除带有 .htaccess 的 .php 扩展名?

标签 .htaccess url-rewriting

我只需要重写一个名为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/

相关文章:

.htaccess - 重写 AJAX 抓取规则

php - 想在 codeigniter 中创建动态子域?

linux - htaccess 规则在 httpd.conf 中工作时不起作用

php - htaccess url 重写许多链接并保护 css 和 js 文件

asp.net - ASP.NET URL重写

c# - 标题中的 URL 重写和 html 规范链接,以避免搜索引擎出现重复页面问题

linux - 运行 Apache/Linux 的 AWS EC2 中的密码保护目录

linux - .htaccess 将 index.php 重定向到/

php - GET参数无法使用php检索

.htaccess - 使用 URL 重写时出现问题(相对路径不起作用)