我们有一个网站正在迁移到 Google App Engine Flex Env。网站上的大部分页面都重定向到了某个地方,但仍有一些页面仍然可以访问。
这是我们 LAMP 系统上的 htaccess 文件
RewriteEngine on
RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png|css|js)$
RewriteCond %{REQUEST_URI} !^/About/.*$
RewriteCond %{REQUEST_URI} !^/Services/.*$
RewriteCond %{REQUEST_URI} !^/Newsroom/.*$
RewriteCond %{REQUEST_URI} !^/Resources/.*$
RewriteCond %{REQUEST_URI} !^/Internal/.*$
RewriteCond %{REQUEST_URI} !^/Contact/.*$
RewriteCond %{REQUEST_URI} !^/Training/.*$
RewriteCond %{REQUEST_URI} !^/content/.*$
RewriteCond %{REQUEST_URI} !^/videos/.*$
RewriteCond %{REQUEST_URI} !^/app/auth/login.*$
RewriteRule (.*) https://example.com [R=301,L]
RewriteCond %{HTTP_HOST} ^site\.com$
RewriteRule ^app/auth/login$ https://another.site.com/? [R=301,L]
是否可以在 app.yaml
中使用 url 处理程序(如 htaccess 的正则表达式)来实现此逻辑,并让它路由到硬编码的 URL 而不是指向脚本?
最佳答案
更新:抱歉,下面的答案是针对 App Engine 标准的。我错过了 Flex
部分。以下是如何在 Flex Env
中执行此操作:
查看示例:https://github.com/GoogleCloudPlatform/getting-started-php
app.yaml:
runtime: php
env: flex
runtime_config:
document_root: web
/web/index.php:
<?php
require_once __DIR__ . '/../vendor/autoload.php';
$app = new Silex\Application();
$app->get('/app/auth/login{somestr}', function($somestr) {
header('Location: https://www.someothersite.com/somewhere{$somestr}');
exit();
});
$app->get('/', function() {
return 'Hello World';
});
$app->get('/{oldDir}/{oldPath}', function($oldDir, $oldPath) {
switch ($oldDir) {
case "About":
header('Location: https://www.someothersite.com/{$oldDir}/{$oldPath}');
exit();
break;
case "videos":
header('Location: https://www.someothersite.com/new_videos/{$oldPath}');
exit();
break;
.....
default:
handle other urls
}
})
/*
Depending on how many other paths there are, you may want to use
separate handlers for each (About, Services, etc.) instead of the
switch function, like:
*/
$app->get('/About/{oldPath}', function($oldPath) {
header('Location: https://www.someothersite.com/NewAbout/{$oldPath}');
exit();
})
?>
对于 App Engine 标准环境:
不,但是是的。 app.yaml
仍然需要指向一个脚本,但该脚本可以进行重定向:
handlers:
- url: /.*\.(gif|jpe?g|png|css|js)$
script: redirect.php
- url: /(About|Services|Newsroom|...videos)/.*$
script: redirect.php
- url: /app/auth/login.*
script: redirect.php
然后让您的 redirect.php
脚本执行重定向:
<?php
header('Location: https://www.someothersite.com' + $_SERVER['REQUEST_URI']);
exit();
?>
您可以进行一些正则表达式匹配或 if/then 逻辑来查看 url 是否用于图像等,并为每个条件设置不同的重定向 url。
关于php - 将 .htaccess 转换为 Google App Engine Flex(301 重定向),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52170146/