How to Force 'WWW' on Google App Engine?

2019-09-05 14:25发布

How to force 'WWW' on Google App Engine? For my PHP scripts, I'm using:

<?php
if (substr($_SERVER['SERVER_NAME'], 0, 4) != 'www.') {
    header("Location: https://www.{$_SERVER['SERVER_NAME']}{$_SERVER['REQUEST_URI']}");
}
?>

But obviously, this wouldn't work for static files such as robots.txt and sitemap.xml.

e.g. https://example.com/robots.txt would not redirect to https://www.example.com/robots.txt e.g. https://example.com/sitemap.xml would not redirect to https://www.example.com/sitemap.xml

At first, I thought I could resolve the issue by only pointing the subdomain (www.example.com) to Google App Engine, but then I realized the naked domain (example.com) wouldn't be reachable.

Please advise.

1条回答
Root(大扎)
2楼-- · 2019-09-05 15:02

I did a redirect for the naked domain to the WWW subdomain using my DNS provider's service.

The WWW subdomain then maps to the App Engine instance.

You can visit gflip.net as an example.

The drawback of this approach is that the naked domain is never used, but this is not been a problem for me at least - it even works for SSL connections.

Hope this helps.

查看更多
登录 后发表回答