3

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?

good_afternoon
  • 1,529
  • 1
  • 12
  • 41

1 Answers1

3

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.

GAEfan
  • 11,244
  • 2
  • 17
  • 33
  • really appreciate your response. Can you give me an example of what redirect.php contents would be? Checking for a $_GET request of the url and just setting header()? Does this have to tie into app.yaml? thanks again – good_afternoon Sep 04 '18 at 16:23
  • Yes, could be as simple as that. Do you have any query strings? Check this answer for alternatives: https://stackoverflow.com/questions/768431/how-to-make-a-redirect-in-php – GAEfan Sep 04 '18 at 16:33
  • Thanks. I don't think so. Since those three conditions all go to the same script, i'd have to test for each with another regular expression or do I somehow have access to the URL handler from app.yaml in my redirect.php script? sorry if that sounds dumb. still learning GAE – good_afternoon Sep 04 '18 at 16:39
  • See edit. Several ways to do this, but this is simplest. – GAEfan Sep 04 '18 at 16:47
  • gotcha. very simple. thanks for your time. I have two different sites to redirect to depending on GET url, but i suppose I can just create a different version of redirect.php and link to there. wish there was a way to test this locally...thanks again. will mark as accepted once I test out (not that i think it will fail) – good_afternoon Sep 04 '18 at 16:49
  • Yes, you can do some more regex testing in app.yaml, then send to different redirect scripts. Or, you can have one redirect.php, and branch off the urls (using regex or if/then filters) there. – GAEfan Sep 04 '18 at 16:51
  • Hi I'm having issues implementing this. once deployed, my url handler doesn't seem to do anything. Is there a way to test app.yaml and this routing locally? It takes like 10-15 minutes to deploy. also, should my redirect.php file be in what i defined as the document_root or on the same level as app.yaml? (right now its on app.yaml level) – good_afternoon Sep 05 '18 at 13:28
  • after some digging it doesn't look like this works for the flex env – good_afternoon Sep 05 '18 at 14:12
  • I tried on the root level (and web folder) and nothing happened. From docs: https://cloud.google.com/appengine/docs/flexible/php/reference/app-yaml theres no mention of handlers. On standard, however, there is: https://cloud.google.com/appengine/docs/standard/php/config/appref. Flex I think needs to use a routing library: https://cloud.google.com/appengine/docs/flexible/php/quickstart. silex is mentioned here. i could be wrong, but nothing worked with url handlers – good_afternoon Sep 05 '18 at 16:31
  • thanks, while it certainly doesn't hold water to .htaccess, i was able to redirect to the site I needed when a route was hit. i'll mark as accepted. side-note: can I test an app engine flex env locally? Do I have to deploy each time? – good_afternoon Sep 05 '18 at 21:30