提供PHP文件时删除PHP文件扩展名 [英] Removing PHP file extension when serving PHP files
问题描述
我正在尝试从我站点中所有*.php
文件中删除地址中的文件扩展名.例如,如果用户访问mysite.com/about.php
,我希望URL读取mysite.com/about
.
I'm trying to remove the file extension in the address from all *.php
files in my site. For instance, if a user visits mysite.com/about.php
I want the URL to read mysite.com/about
.
这是我的app.yaml
:
application: mysite
version: 1
runtime: php55
api_version: 1
handlers:
# Static files
- url: /images/*
static_dir: images
- url: /css/*
static_dir: css
- url: /js/*
static_dir: js
# Routing
- url: /services(.*)
script: services.php
- url: /portfolio(.*)
script: portfolio.php
- url: /project(.*)
script: project.php
- url: /about(.*)
script: about.php
- url: /contact(.*)
script: contact.php
- url: /(.*)
script: index.php
如何在GAE中实现这一目标?
How could I achieve this within GAE?
推荐答案
如果您所需要做的只是执行这种类型的路由,则可以使用以下方法:
If all you need to do is perform that type of routing, you could use this:
application: mysite
version: 1
runtime: php55
api_version: 1
handlers:
# Static files
- url: /images/*
static_dir: images
- url: /css/*
static_dir: css
- url: /js/*
static_dir: js
# Special case, route requests for the root document
- url: /
script: /index.php
# If the request ends in [something].php, route to that file directly
- url: /(.+\.php)$
script: /\1
# Map all other requests to scripts of the same name
# so that, eg, /thispage (or /thispage/) routes to /thispage.php
- url: /(.*?)/?$
script: /\1.php
但是,使用此方法,您将无法处理404错误(尽管问题中提供的app.yaml也似乎未处理这些错误).您的app.yaml中的错误处理程序不适用于404,因为它们只会在无法匹配您提供的任何路线时才会加入.
NB however that using this method you won't be able to handle 404 errors (although they don't appear to be handled in the app.yaml provided in the question either). Error-handlers in your app.yaml won't work for a 404 because they'll only kick in if they can't match any route you've provided.
因此,如果您想处理404错误等,您应该做的是从php脚本内部进行路由,如下所示:
So, if you want to handle 404 errors and such, what you should do instead is do the routing from inside a php script, like this:
-url: /(.*)
script: /routes.php
在routes.php内部,检查$ _SERVER ['REQUEST_URI']变量以查看请求的页面,并相应地提供适当的内容.
and inside routes.php inspect the $_SERVER['REQUEST_URI'] variable to see what page was requested, and serve the appropriate content accordingly.
edit: thanks to @Avinash Raj for cleaning up the regex Here
这篇关于提供PHP文件时删除PHP文件扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!