views:

219

answers:

2

With the new Diggbar, you can put http://digg.com in front of any URL that you are currently at and it will create a Digg short URL. I am only assuming they do this by modrewrite (though I am not sure since I am new at this all).

How is this done? It seems to me when I try this with a website I am working on, it bombs out.

I want to be able to do the following:

http://example.com/http://stackoverflow.com/question/ask

and have a modrewrite that will allow this to go to

http://example.com/index.php?url=http://stackoverflow.com/question/ask

But when I use this modrewrite:

RewriteEngine on
RewriteRule ^([A-Za-z0-9]+)$ /message.php?id=$1 [L]

it doesn't work. What am I doing wrong?

+1  A: 

You're only looking for letters and numbers in that regular expression, so it won't pick up the colon and slashes. You're also using index.php in the example and message.php in the htaccess ;)

You'll probably want something like this:

RewriteEngine on
RewriteRule ^http://(.+)$ /index.php?url=$1 [L]

This makes sure you only catch URLs here, and you can still have regular pages! (Think about what would have happened if you went to example.com/index.php, you'd end up in an infinite loop.)

DisgruntledGoat
you might want to take into account https
Tom Haigh
Very good. I am never good at re-reading my posts for little catches like index.php instead of messages.php. As for taking into consideration https:// and http://, what steps would I need to take for that?
JoshFinnie
you might be able to just change the regular expression to ^https?://(.+)$
Tom Haigh
Take a look at `RewriteMatch` too - it might help
atc
+2  A: 

You have to take the value from the request line because Apache removes empty path segments. The initially requested URI path /http://foobar/ becomes /http:/foobar/. But the request line (THE_REQUEST) stays untouched:

RewriteCond %{THE_REQUEST} ^GET\ /(https?://[^\s]+)
RewriteRule ^https?:/ index.php?url=%1 [L]
Gumbo
Excellent. Just had to add the "RewriteEngine on" and it worked! Thank you!
JoshFinnie