将.htaccess转换为Google App Engine Flex(301重定向)

时间:2018-09-04 15:59:13

标签: php google-app-engine

我们有一个网站要迁移到Google App Engine Flex环境。该网站上的大多数页面都重定向到某个地方,但仍有一些页面仍可访问。

这是我们的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这样的reg表达式)使用此逻辑,并将其路由到硬编码URL而不是指向脚本?

1 个答案:

答案 0 :(得分:2)

更新:很抱歉,以下答案适用于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。