【问题标题】:Convert .htaccess to Google App Engine Flex (301 redirects)将 .htaccess 转换为 Google App Engine Flex(301 重定向)
【发布时间】:2018-09-04 15:59:12
【问题描述】:

我们有一个网站正在迁移到 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 的 reg 表达式)拥有此逻辑,并将其路由到硬编码的 URL 而不是指向脚本?

【问题讨论】:

    标签: php google-app-engine


    【解决方案1】:

    更新:抱歉,下面的答案是针对 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。

    【讨论】:

    • 非常感谢您的回复。你能给我一个redirect.php内容的例子吗?检查 url 的 $_GET 请求并设置 header()?这是否必须与 app.yaml 相关联?再次感谢
    • 是的,就这么简单。你有任何查询字符串吗?检查此答案以获取替代方案:stackoverflow.com/questions/768431/…
    • 谢谢。我不这么认为。由于这三个条件都转到同一个脚本,我必须用另一个正则表达式测试每个条件,或者我是否可以在我的 redirect.php 脚本中从 app.yaml 访问 URL 处理程序?抱歉,如果这听起来很愚蠢。还在学习 GAE
    • 查看编辑。有几种方法可以做到这一点,但这是最简单的。
    • 明白了。很简单。谢谢你的时间。根据 GET url,我有两个不同的站点要重定向到,但我想我可以创建一个不同版本的 redirect.php 并链接到那里。希望有一种方法可以在本地进行测试...再次感谢。一旦我测试出来,将标记为已接受(不是我认为它会失败)
    猜你喜欢
    • 2012-09-22
    • 2016-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-07
    • 1970-01-01
    • 2011-04-14
    相关资源
    最近更新 更多