Convert .htaccess to Google App Engine Flex (301 redirects)

Viewed 292

We have a site that we're migrating to Google App Engine Flex Env. Most of the pages on the site are redirecting somewhere, but there's a few that remain accessible.

Here is our htaccess file on our LAMP system

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]

Is it possible to have this logic in app.yaml with a url handler (reg expression like htaccess) and have it route to a hard coded URL instead of pointing to a script?

1 Answers

UPDATE: Sorry, the answer below was for App Engine Standard. I missed the Flex part. Here is how to do this in Flex Env:

See examples at: 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();
    })

?>

For App Engine Standard Env:

No, but yes. The app.yaml would still need to point to a script, but that script could do the redirect:

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

Then have your redirect.php script do the redirecting:

<?php 

    header('Location: https://www.someothersite.com' + $_SERVER['REQUEST_URI']);
    exit();

?>

You can do some regex matching or if/then logic to see if the url is for an image, etc., and set different redirect urls for each condition.

Related