简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM