开发者

redirecting urls that contain the "?" character in the file names

I'm a novice php/mysql developer and need some help.

I was working on a project with a .NET developer. He built a URL redirecting function in .NET that uses the ? in the URL (eg, mysite.com?123 redirects to www.realsite.com. This developer has now disappeared on me and I no longer have access to the server.

I need to move my domain to a new server and recreate this functionality in ph开发者_StackOverflow社区p/mySQL. I have about 40 urls that I need to support using this technique (they're embedded in QR codes). I don't need a solution that generates new redirects this way - I've got a different solution going forward.

My new server will be on bluehost.

Many thanks to anyone who can help me.


To replicate the original setup you dond't need to involve PHP at all. You can accomplish this at the server level using some RewriteRules:

RewriteCond %{QUERY_STRING} ^123$
RewriteRule ^$ http://realsite.com/

See also http://wiki.apache.org/httpd/RewriteQueryString


The query string can be found in $_SERVER['QUERY_STRING'].


On your old host for mysite.com domain create a .htaccess file in DOCUMENT_ROOT like this:

Options +FollowSymlinks -MultiViews
RewriteEngine on

# for HTTP traffic
RewriteCond %{QUERY_STRING} !^$
RewriteCond %{SERVER_PORT} =80
RewriteRule ^/?$ http://www.realsite.com/? [R=301,L]

# for HTTPS traffic
RewriteCond %{QUERY_STRING} !^$
RewriteCond %{SERVER_PORT} =443
RewriteRule ^/?$ https://www.realsite.com/? [R=301,L]

Above code will redirect every URI that looks like mysite.com?foo to http://www.realsite.com/ with HTTP status code 301.

? in the end will discard any existing QUERY string.


http://example.com?123

would be the equivalent of doing

http://example.com/index.php?123

Your choices would be either to catch those codes via mod_rewrite and redirect the request to a script specifically set up to handle these legacy urls. Or you can modify the index.php script to look for the codes and do whatever it has to at that point.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜