0

I'd like to make shortened links for my site to be used in Tweets. I'm interested in a t.co-like URLs but confused on how to implement the redirect.

Here's how a link on my site typically looks:

https://mysite.com/item/this-is-a-book-on-toasters

Here's how I'd like the shortened link to look which would redirect to the above link

https://ms.co/Im8y2x

Based on googling how to do this, it looks I need to do a 301 redirect. I'm using PHP, specifically Codeigniter and I guess there is 2 components: the PHP script and .htaccess.

Here's my .htaccess:

RewriteEngine on
RewriteRule ^/?$ https://mysite.com [L]
RewriteRule ^(.*)$ https://ms.co/$1 [R=301,NC]

The PHP I think I need is in here.

Unfortunately, I can't interpret the answers on this link to make a useful script. Might someone help with this? Also, does my .htaccess look right?

Community
  • 1
  • 1
tim peterson
  • 23,653
  • 59
  • 177
  • 299

2 Answers2

1

This should be the .htaccess code on your shortlinking website (ms.co):

RewriteEngine On

RewriteRule ^(.*)$ https://mysite.com/in.php?id=$1 [R=301,L]

The in.php should contain the script that decodes the $_GET['id'] (via the short hash decoding methods supplied in the link you supplied), matches it against an ID into your database, and retrieves page that it should redirect to.

By the way, the reason I didn't add a NC part in the code is because upper/lowercase (often) can yield different results when using decoding methods.

Destralak
  • 418
  • 2
  • 7
  • thanks @Destralak, I'm using Codeigniter so there is no `.php` at the end of my URLs. Can you correct your `RewriteRule` to reflect that? Also I only have 1 website, mysite.com. I've only bought the name for ms.co but don't know how to redirect it to mysite.com. Can you comment on this too? – tim peterson Jul 11 '12 at 11:28
  • You can modify or remove the in.php as you wish. If you have bought webhosting for ms.co you would be able to put the htaccess file in the http root directory. If that's not the case, then the required actions depends on the package that you've bought. Send me a PM. – Destralak Jul 11 '12 at 13:21
  • thanks @Destralak, I don't know to send you a PM, can you explain how you'd like to proceed? – tim peterson Jul 11 '12 at 15:17
  • Woops, you can't send a PM here :-) Send me your details at stackoverflow [at] xnh [dot] nl – Destralak Jul 12 '12 at 07:53
  • hi @Destralk, thanks I sent an email to stackoverflow@xnh.nl, my email is petersontimr@gmail.com. Let me know if you don't get it. – tim peterson Jul 12 '12 at 15:41
0

I've anaswered my own question. Here's what they .htaccess directive should look like given the question above:

RewriteCond %{HTTP_HOST} ^o-a\.co$ [NC] // the rewrite rule
RewriteCond %{HTTPS} =on //to enable HTTPS
RewriteRule ^(.*)$ https://mysite.com/page-to-handle-hash/$1 [L] //where in your application you want to send the 6-digit hash
tim peterson
  • 23,653
  • 59
  • 177
  • 299