Configurer une redirection d'URL pour un équilibreur de charge d'application classique
Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Cet exemple montre comment utiliser des redirections d'URL pour rediriger toutes les requêtes entrantes vers un hôte ou un chemin d'accès différent.
Contenu associé
Cette page est réservée aux équilibreurs de charge d'application classiques. Si vous utilisez un équilibreur de charge en mode différent, consultez l'une des pages suivantes :
Pour un équilibreur de charge de type HTTP(S) (classique), cliquez sur le lien du nom de l'équilibreur de charge.
Cliquez sur Modifieredit.
Dans Règles d'hôte et de chemin d'accès, sélectionnez Règle d'hôte et de chemin d'accès avancée (redirection et réécriture d'URL).
Cliquez sur Ajouter une règle d'hôte et de chemin d'accès.
Dans le champ Hôtes, saisissez *.
Cliquez sur edit.
Pour l'action, sélectionnez Redirigez le client vers un autre hôte/chemin d'accès.
Pour la redirection d'hôte, saisissez un nom d'hôte vers lequel effectuer la redirection ou omettez-le pour conserver l'hôte demandé.
Sous Redirection de chemin, sélectionnez Redirection de chemin d'accès complet.
Dans le champ Valeur du chemin, saisissez un chemin vers lequel effectuer la redirection ou omettez-le pour conserver le chemin demandé.
Dans le champ Retirer la requête, cliquez sur Activer.
Cliquez sur Enregistrer pour terminer la configuration de la règle d'hôte et de chemin d'accès.
Cliquez sur OK pour terminer la modification du mappage d'URL.
Cliquez sur Mettre à jour pour terminer la modification de l'équilibreur de charge.
gcloud
L'exemple suivant renvoie un code de réponse 3xx configurable, définit l'en-tête de réponse Location avec l'URI approprié, et remplace l'hôte et le chemin d'accès comme spécifié dans l'action de redirection.
Mettez à jour le fichier de mappage d'URL lb-map-config.yaml en ajoutant ce qui suit à la fin du fichier :
hostRules:-hosts:-'*'pathMatcher:path-matcher-1name:web-map-httppathMatchers:-defaultUrlRedirect:hostRedirect:NEW_HOSTNAME# Omit to keep the requested hosthttpsRedirect:falsepathRedirect:/NEW_PATH# Omit to keep the requested pathredirectResponseCode:MOVED_PERMANENTLY_DEFAULTstripQuery:truename:path-matcher-1
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/04/22 (UTC).
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","otherDown","thumb-down"]],["Dernière mise à jour le 2025/04/22 (UTC)."],[],[],null,["# Set up a URL redirect for a classic Application Load Balancer\n\nThis example demonstrates how to use [URL\nredirects](/load-balancing/docs/url-map-concepts#url-redirects) to redirect all\nincoming requests to a different host or path.\n\nRelated content\n---------------\n\nThis page is for a classic Application Load Balancer only. If you are using a load\nbalancer in a different mode, see one of the following pages:\n\n- [Configure a URL redirect for\n global external Application Load Balancers](/load-balancing/docs/https/setting-up-global-traffic-mgmt#configure_a_url_redirect)\n\n- [Configure a URL redirect for\n regional external Application Load Balancers](/load-balancing/docs/https/setting-up-reg-traffic-mgmt#configure_a_url_redirect)\n\nIf you're trying to set up an HTTP-to-HTTPS redirect, see [Set up an\nHTTP-to-HTTPS redirect for a\nclassic Application Load Balancer](/load-balancing/docs/https/setting-up-http-https-redirect).\n\nAdd a URL redirect\n------------------\n\n### Console\n\n1. Go to your list of load balancers in the Google Cloud console. \n [Go to Load balancing](https://console.cloud.google.com/networking/loadbalancing/list/loadBalancers)\n2. For a load balancer of type **HTTP(S) (Classic)**, click the load balancer's name link.\n3. Click **Edit** edit.\n4. In **Host and path rules** , select **Advanced host and path rule (URL\n redirect, URL rewrite)**.\n5. Click **Add host and path rule**.\n6. In the **Hosts** field, enter **\\***.\n7. Click edit.\n8. For the **Action** , select **Redirect the client to different\n host/path**.\n9. For the host redirect, enter a hostname to redirect to, or omit to keep the requested host.\n10. For **Path redirect** , select **Full path redirect**.\n11. For **Path value**, enter a path to redirect to, or omit to keep the requested path.\n12. For **Strip query** , click **Enable**.\n13. Click **Save** to finish configuring the host and path rule.\n14. Click **Done** to finish editing the URL map.\n15. Click **Update** to finish editing the load balancer.\n\n### gcloud\n\nThe following example returns a configurable 3xx response code, sets\nthe `Location` response header with the appropriate URI, and replaces the\nhost and path as specified in the redirect action.\n\n1. Export the existing URL map using the [`gcloud compute url-maps\n export`](/sdk/gcloud/reference/compute/url-maps/export) command:\n\n ```\n gcloud compute url-maps export web-map-http \\\n --destination=lb-map-config.yaml \\\n --global\n ```\n2. Update the URL map file `lb-map-config.yaml` by adding this to the\n end of the file:\n\n hostRules:\n - hosts:\n - '*'\n pathMatcher: path-matcher-1\n name: web-map-http\n pathMatchers:\n - defaultUrlRedirect:\n hostRedirect: \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-l devsite-syntax-l-Scalar devsite-syntax-l-Scalar-Plain\"\u003eNEW_HOSTNAME\u003c/span\u003e\u003c/var\u003e # Omit to keep the requested host\n httpsRedirect: false\n pathRedirect: /\u003cvar translate=\"no\"\u003eNEW_PATH\u003c/var\u003e # Omit to keep the requested path\n redirectResponseCode: MOVED_PERMANENTLY_DEFAULT\n stripQuery: true\n name: path-matcher-1\n\n3. Update the URL map using the [`gcloud compute url-maps\n import`](/sdk/gcloud/reference/compute/url-maps/import) command:\n\n ```\n gcloud compute url-maps import web-map-http \\\n --global \\\n --source=lb-map-config.yaml\n ```\n\nWhat's next\n-----------\n\n- [Clean up the load balancer setup](/load-balancing/docs/cleaning-up-lb-setup)."]]