从输出中删除.php扩展



我正在用Perch开发一个小型CMS解决方案。它目前在我的本地开发计算机上的WampServer上运行。

由于 Perch 不提供开箱即用的友好 URL,我想实现这一点,同时确保/perch 目录保持不变。

到目前为止,我已经完成了重写部分,即对/blog.php 的请求将 301 更改为/blog,并且/blog 将重写为/blog.php,使用以下规则:

Options +FollowSymLinks -MultiViews
RewriteEngine On
# Rewrites domiain.com/file to domain.com/file.php
RewriteCond %{REQUEST_URI} !^/perch
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php 
# Redirects domain.com/file.php to domain.com/file
RewriteCond %{REQUEST_URI} !^/perch
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} ^(.+).php$
RewriteRule (.*).php$ /$1 [R=301,L]

但是,我仍然在 HTML 输出中留下.php扩展。我尝试将以下内容添加到我的 .htaccess 文件中:

AddOutputFilterByType SUBSTITUTE text/html
#Replace all .php extensions
Substitute s|.php||ni
#Original blog pattern /blog/post.php?s=2014-11-18-my-first-blog-post
Substitute s|blog/post?s=(w+)|blog/$1|i

但是,这是全局应用的,即甚至应用于/perch 文件夹中的链接。无论如何,我都找不到添加一个条件来将其应用于除/perch 文件夹以外的所有内容 - 有这样的方法吗?

我还查看了 ProxyPass/ProxyReversePass 文档,但这似乎有点矫枉过正,只是替换页面上的一些 HTML。

任何帮助将不胜感激。

亲切问候多特德夫

你说的是 www.grabaperch.com 的 Perch CMS 吗?

一切都在这里:http://docs.grabaperch.com/video/v/simple-url-rewriting/

但是,我仍然在 HTML 输出中留下.php扩展

.htaccess/mod_rewrite 不会对您的 HTML 输出执行任何操作。

将重写规则视为将邮件 (URL) 传递到目标邮箱(实际文件)的邮递员。

您要做的是"手动"省略标记中的.php扩展名(HTML 输出):

  • 在 perch_pages_navigation() 中,您需要将hide-extensions设置为 true
  • 您手动添加的网址:只需编写即可.php

现在,您需要指示邮递员将这些地址路由到.php文件。这就是这些重写规则的用途。所以.htaccess不会删除.php后缀 - 相反,它会添加它。

这是Perch(或任何"删除.php"用例)+ Perch博客的基本.htaccess(进入您的public_html目录)。我添加了一些解释:

# make sure the address we received (e.g. /mypage) is not an existing file      
RewriteCond %{REQUEST_FILENAME} !-f
#  make sure it's not an existing directory either
RewriteCond %{REQUEST_FILENAME} !-d
# make sure there IS an existing .php file corresponding to it
RewriteCond %{REQUEST_FILENAME}.php -f
# if the address starts with "blog/", pick what comes afterwards, put it into the GET Parameter and quit (that's the [L]) 
RewriteRule ^blog/([a-zA-Z0-9-/]+)$ /blog/post.php?s=$1 [L]
# if the first conditions are ok, but it wasn't a blog post (else we would have quit), just append .php to it. Ah, and keep other get params (that's the QSA=Query String Append). 
RewriteRule ^(.+)$ $1.php [L,QSA]

对于更精细的可能性,您可以从这里开始:https://github.com/PerchCMS/perchdemo-swift/blob/master/public_html/.htaccess

这对 CMS 在 /perch/ 中的功能完全没有影响。

最新更新