[英]Beautify URL on Google App Engine PHP Static Website
I am developing a static website for my friend with some light vanilla PHP code.我正在为我的朋友开发一个带有一些轻量级 PHP 代码的静态网站。 There is a .php on the url on every page such as xxxxx.com/service.php.
每个页面的 url 上都有一个 .php,例如 xxxxx.com/service.php。 I did some research and found out that tweaking on
app.yaml
is not an elagance method as it will not handling error 404 gracefully.我做了一些研究,发现对
app.yaml
进行调整并不是一种优雅的方法,因为它不会优雅地处理 404 错误。 What is in my mind is that I can create a special PHP file taking in request that end with .php from app.yaml
and process each request like .htaccess
.我的想法是,我可以创建一个特殊的 PHP 文件,接收以 .php 结尾的来自
app.yaml
请求,并处理每个请求,如.htaccess
。 Due to my lacking of PHP knowledge, I am not able to produce the code in the file.由于我缺乏 PHP 知识,我无法在文件中生成代码。 Please enlighten me on the process.
请赐教过程。
Below is my app.yaml
下面是我的
app.yaml
runtime: php55
api_version: 1
instance_class: F1
automatic_scaling:
max_idle_instances: 1
min_pending_latency: 30ms
max_instances: 1
handlers:
- url: /css
static_dir: css
secure: always
- url: /js
static_dir: js
secure: always
- url: /images
static_dir: images
secure: always
- url: /fonts
static_dir: fonts
secure: always
- url: /sitemap\.xml
static_files: sitemap.xml
upload: sitemap.xml
secure: always
- url: /
script: index.php
secure: always
- url: /(.+\.php)$
script: \1
secure: always
- url: /.*
script: 404.php
secure: always
If you are sure all your static files are already handled in app.yaml
, and root ( /
) is handled, that means anything left would go to a script.如果您确定所有静态文件都已在
app.yaml
处理,并且根 ( /
) 已处理,则意味着剩下的任何内容都将进入脚本。 So, your final handler could be:因此,您的最终处理程序可能是:
- url: /(.+)
script: \1.php
I would leave the我会离开
- url: /(.+\.php)$
script: \1
handler above it, as the penultimate handler, so a person can go to /about
or /about.php
and it will handle both.它上面的处理程序,作为倒数第二个处理程序,所以一个人可以去
/about
或/about.php
并且它会处理这两个。
- url: /(.+)\.php$
script: \1.php
Perhaps redirect the .php
version to beautified, so users adapt over time without getting a 404
:也许重定向
.php
版本以进行美化,以便用户随着时间的推移而适应而不会出现404
:
- url: /(.+)\.php$
script: redirect.php
redirect.php:重定向.php:
<?php
require_once __DIR__ . '/../vendor/autoload.php';
$app = new Silex\Application();
$app->get('/{scriptName}.php', function($scriptName) {
header('Location: https://www.example.com/NewAbout/{$scriptName}');
exit();
})
?>
Update: To handle 404
s, you need a list (like you would in .htaccess) of valid URLs to catch.更新:要处理
404
,您需要一个有效 URL 列表(就像在 .htaccess 中一样)来捕获。 You can do this using regex in app.yaml
.您可以使用
app.yaml
正则表达式来执行此操作。 Then, to handle 404
s, you finish with a catchall handler:然后,要处理
404
秒,您可以使用 catchall 处理程序完成:
- url: /(home|about|contact|page1|page99|etc)$ ## the pipe (|) means "or"
script: \1.php
- url: /.*
script: 404.php
Then you write a simple 404.php script page to display the proper message and deliver a 404
http response.然后编写一个简单的 404.php 脚本页面来显示正确的消息并传递
404
http 响应。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.