简体   繁体   中英

How can I forward a dynamic URL to twitter using .htaccess or PHP?

I need to rewrite a dynamic URL. The content myurl and mytext is always different and should be insert in the "text" and "url" from the Twitter string.

This is the string:

http://example.com/share/?myurl=http%3A%2F%2Fwww.example.com&mytext=helloworld
   /* forward to: */
http://twitter.com/intent/tweet?related=Example%3Aname&text=helloworld&url=http%3A%2F%2Fwww.example.com&via=example

How can that be made? (I browsed the .htaccess suggestions but couldn't find a solution for my specific problem.)

Does your server support PHP?

You could just put something like the following in a PHP file:

Header("Location: http://twitter.com/intent/tweet?related=Example%3Aname&text=$_GET['mytext']&url=$_GET['myurl']&via=example");

It will be more efficient to handle it with rewrite Rules.

RewriteEngine On
RewriteCond %{QUERY_STRING}  ^myurl=(.*)&mytext=(.*)$
RewriteRule ^(.*)$ http://twitter.com/intent/tweet?related=Example%3Aname&text=%2&url=%1&via=example [R,L]

If you want this to be permanent redirect then replace R with R=301.

With permanent redirects, these links will always be redirected by browser and your server will have to deal with less traffic if that is desired.

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