如何将友好 URL 重定向到动态 URL?

发布于 2024-10-05 13:51:11 字数 236 浏览 0 评论 0原文

将 Linux 上看起来友好的 URL 动态重定向到 Windows 上的动态 URL 的最佳方法是什么?

例如
domainone.com/dir/one/two 重定向到 domaintwo.com/index.aspx?a=one&b=two

domainone.com/dir/third/four 重定向到 domaintwo.com/index.aspx?a=三&b=四

What is the best way to dynamically redirect a friendly looking url on Linux to dynamic on Windows?

ex.
domainone.com/dir/one/two
redirects to
domaintwo.com/index.aspx?a=one&b=two

and

domainone.com/dir/three/four
redirects to
domaintwo.com/index.aspx?a=three&b=four

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深陷 2024-10-12 13:51:11

名为 Location 的 HTTP 标头是重定向用户的方式跨主机/域。

根据您的服务器配置和用于生成 HTTP 标头的机制,具体实现会有所不同。 PHP 中的一个示例(您的问题似乎已标记)包括以下代码:

header('Location: http://domaintwo.com/index.aspx?a=one&b=two');

上面的字符串与任何其他字符串一样,因此应用适当的逻辑来提供所需的重定向 URL。

在域配置文件中(精确路径因服务器软件和操作系统而异)或更传统的 .htaccess 文件中也可能产生相同的效果。如果您提供有关托管环境的更多信息,有人将能够帮助您设计所需的重写规则。我更喜欢将这种级别的智能重写放入 PHP 脚本中,因为我认为 .htaccess 文件往往更难管理和“读取”。

The HTTP header called Location is how you redirect a user across hosts/domains.

Depending on your server configuration and the mechanism used to generate the HTTP headers, the specific implementation will vary. An example in PHP (as your question appears to be tagged) is to include the following code:

header('Location: http://domaintwo.com/index.aspx?a=one&b=two');

The string above is like any other string, so apply the appropriate logic to provide the desired redirection URL.

The same effect is also possible in the domain configuration files (the precise path differs across server software and operating system) or more conventionally in .htaccess files. If you provide more information about your hosting environment, someone will be able to help you devise the rewrite rule you need. I prefer to put this level of smart rewriting in a PHP script, since I think .htaccess files tend to be harder to manage and "read".

ぺ禁宫浮华殁 2024-10-12 13:51:11

从 Apache 内部:

在服务器配置文件中,或更可能在 .htaccess 文件中。

您可以使用 mod_rewrite 来执行此操作,但是当您想要重定向时,使用 mod_alias 和 RedirectMatch 语句会更合适。

RedirectMatch 301 ^/a/([^/]+)/([^/]+)$ http://domaintwo.com/index.aspx?a=$1&b=$2

重写变体:

RewriteEngine On
RewriteBase /
RewriteRule ^/a/([^/]+)/([^/]+)$ http://domaintwo.com/index.aspx?a=$1&b=$2 [R=301,L]

请注意 301 的使用,即永久重定向,使用 302 进行临时重定向,或者当您始终希望人们重定向而不是直接进行将来的访问时。

From within Apache:

Either in a server configuration file, or more likely in an .htaccess file.

You can use mod_rewrite to do this, but as you want a redirect, it would be more appropriate to use mod_alias and the RedirectMatch statement.

RedirectMatch 301 ^/a/([^/]+)/([^/]+)$ http://domaintwo.com/index.aspx?a=$1&b=$2

Rewrite variant:

RewriteEngine On
RewriteBase /
RewriteRule ^/a/([^/]+)/([^/]+)$ http://domaintwo.com/index.aspx?a=$1&b=$2 [R=301,L]

Note the use of 301, that is a permanent redirect, use 302 for temporary, or when you always want people to redirect rather than going directly on future accesses.

深巷少女 2024-10-12 13:51:11

在 Linux 上解决这个问题的一个相当标准的方法是使用 Apache 和 mod_rewrite (如何安装和配置 Apache 和 mod_rewrite,如果尚未设置,将取决于您的 Linux 发行版)

然后,在您的 Apache 配置中,您可以添加线如:

RewriteEngine On
RewriteRule ^/a/([^/]+)/([^/]+) http://www.domaintwo.com/index.aspx?a=$1&b=$2

A pretty standard way to approach this on Linux is to use Apache with mod_rewrite (how to install and configure Apache and mod_rewrite, if it's not already set up, will depend on your Linux distribution)

Then, in your Apache configuration, you can add a line like:

RewriteEngine On
RewriteRule ^/a/([^/]+)/([^/]+) http://www.domaintwo.com/index.aspx?a=$1&b=$2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文