首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从输出中删除.php扩展

从输出中删除.php扩展
EN

Stack Overflow用户
提问于 2014-11-19 06:36:37
回答 1查看 465关注 0票数 0

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

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

到目前为止,我已经完成了重写部分,即对/blog.php的请求将通过301发送到/blog,而/blog将使用以下规则重写到/blog.php:

代码语言:javascript
复制
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]

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

代码语言:javascript
复制
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似乎有点过分了。

任何帮助都将不胜感激。

致以良好的问候,dotdev

EN

回答 1

Stack Overflow用户

发布于 2014-11-20 03:02:23

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

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

但是,我仍然需要在

输出中使用.php扩展

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

可以将RewriteRules看作是一个邮递员,他将邮件(URL)递送到目标邮箱(实际文件)。

您所要做的就是“手动”省略标记( .php输出)中的扩展名:

  • perch_pages_navigation()中,您需要将hide-extensions设置为手动添加的真实
  • URL:只需在不使用.php

的情况下编写它们

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

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

代码语言:javascript
复制
# 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

这对/perch/中的内容管理系统的功能没有任何影响。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27005474

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档