Redirect URLs with Google App Engine: Full Guide
In this article
Yes, you can redirect all URLs using Google App Engine by configuring URL routing rules in your app.yaml file. This allows you to send users and search engines from one URL path to another, such as redirecting HTTP to HTTPS, moving from an old domain to a new one, or enforcing a canonical URL structure. To do this effectively, you need a working App Engine project, a properly configured app.yaml file, and knowledge of HTTP status codes like 301 (permanent) and 302 (temporary).
What You'll Need
- A Google Cloud Platform (GCP) project with Google App Engine enabled
- The Google Cloud SDK installed and configured on your machine
- Access to edit and deploy the app.yaml file
- A domain (if redirecting custom domains) verified in Google Search Console or GCP
- Billing enabled (required for custom domains and some routing features)
Step-by-Step
- Open your project directory and locate the
app.yamlfile. If it doesn’t exist, create one in the root of your application. - Define the runtime environment at the top of the file. For a simple redirect, you can use a lightweight runtime like
python39,nodejs16, orgo119. Example:runtime: python39. - Add a
handlerssection to define how incoming URLs are processed. Each handler specifies a URL pattern and what action to take. - To redirect all URLs, use a catch-all pattern. Add a handler with
url: /.*to match every possible path. - Set the
redirect_http_response_codeto either301for permanent redirects or302for temporary ones. Use 301 for SEO-preserving moves. - Specify the
securesetting to control HTTPS behavior. Usesecure: alwaysto force HTTPS, which automatically redirects HTTP requests. - If redirecting to a different domain, use the
application_readablehandler with a script that issues the redirect. For static redirects without code, usestatic_filesorredirect_http_response_codecombined withsecure. - For full domain-level redirects (e.g., olddomain.com to newdomain.com), configure a custom domain in GCP and use a script-based handler that returns a 301 redirect response.
- Deploy the updated
app.yamlusing the command:gcloud app deploy. Wait for the deployment to complete. - Test the redirect by visiting various URLs on your domain. Confirm that all paths (e.g., /page, /blog/post) redirect correctly with the expected status code.
Common Mistakes
- Forgetting to set
url: /.*— without this catch-all pattern, only specific paths will be redirected. - Using
secure: redirectedinstead ofalways— this does not force HTTPS unless combined with correct handler ordering. - Deploying without billing enabled — custom domain redirects may fail silently if billing is not active.
- Incorrect handler order — App Engine processes handlers sequentially, so a more specific rule above the catch-all can block it.
- Not testing all URL variants — some paths might bypass the redirect if static file handlers are defined above the catch-all rule.
Tips
- Always place the catch-all redirect handler at the end of the
handlerslist to ensure it doesn’t override other routes. - Use 301 redirects for SEO purposes when permanently moving content.
- Verify domain ownership in Google Search Console before attempting domain-level redirects.
- Consider using a minimal runtime like Go or Python to reduce cold start delays.
- Log redirects using App Engine’s logging service to monitor traffic and catch errors.
Troubleshooting
If redirects are not working, first check the app.yaml syntax using gcloud app deploy --novalidate to catch errors. Ensure the catch-all handler is last and that no static file rules are intercepting requests. If using custom domains, confirm DNS settings point to App Engine and the domain is verified in GCP. Check the App Engine logs in the Google Cloud Console for any 5xx or 4xx errors. If the redirect works locally but not in production, redeploy the app and clear any caching layers. If HTTPS is not enforced, verify that secure: always is set and that the app is serving on a domain with SSL enabled.
Frequently Asked Questions
Can I redirect all URLs to a single page?
Yes. Use a catch-all handler with url: /.* and set a redirect to your target page using static_files or a script.
Does Google App Engine support 301 redirects?
Yes. Set redirect_http_response_code: 301 in your handler to issue permanent redirects.
Can I redirect a custom domain with App Engine?
Yes. After verifying the domain in Google Cloud, configure DNS to point to App Engine and use routing rules to redirect traffic.
Do I need code to redirect URLs?
Not always. Simple redirects can be done in app.yaml using secure and redirect_http_response_code. Complex logic may require a small script.
Why are some URLs not redirecting?
Check handler order — earlier rules may intercept requests. Also ensure the catch-all pattern is /.* and not overridden by static file handlers.
Can I redirect without a custom domain?
Yes. You can redirect within the default your-project.appspot.com domain using the same app.yaml rules.