开发者

How do you redirect a friendly url to a dynamic url?

开发者 https://www.devze.com 2023-01-28 15:50 出处:网络
What is the best way to d开发者_开发问答ynamically redirect a friendly looking url on Linux to dynamic on Windows?

What is the best way to d开发者_开发问答ynamically 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


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".


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.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号