0

I'm new to making websites. As a personal project I want to make a URL shortening service. Everything is going OK except for one thing.

I cannot handle incoming requests.

eg. If someone types www.examp.le/Dnd5Je I want to be able to get the last 6 letters from the URL and then look for it in the database.

The problem is I cannot figure out how to get the letters without using the $_GET array and using an identifier such as ?id=77 in the URL.

I'm a bit lost and would be greatful for any help on how to extract the last 6 letters. Thanks

5 Answers 5

3

You can rewrite the URL in a .htaccess file:

RewriteEngine On
RewriteRule ^([^/.]+)/$ index.php?target=$1
RewriteRule ^$ index.php

The above code will rewrite an URL such as www.examp.le/foobar123/ to www.examp.le/index.php?target=foobar123. You can then use the $_GET variable to read the value of $_GET['target'].

2
  • That works perfectly, thank you very much!! I owe you big time! But is there a way that I can leave out the last "/" in the url? Because it won't work without the last "/". Thanks again.. very helpful!
    – Robbie
    Jun 9, 2012 at 14:52
  • Actually, I figured it out.... Just had to remove the last "/" from your code above! Thank you again!
    – Robbie
    Jun 9, 2012 at 15:01
1

You could use basename()

$str = basename($url);

Working example here

1

Try using $_SERVER['REQUEST_URI'].

1

The reason the $_GET array isn't being populated is because what are receiving in www.examp.le/Dnd5Je is a valid URL with no GET information. GET information is found after the question mark (?) as key/value pairs

For you to implement what you are trying to achieve you will need to have your web server "rewrite" the URL. How to do this is dependent on which web server you're using. For Apache the documentation can be found here

http://httpd.apache.org/docs/current/mod/mod_rewrite.html

0

You could use $_SERVER['REQUEST_URI']. It will contain the leading slash, i.e. /Dnd5Je from your example, which you can easily remove.

For more info, consider Manual for $_SERVER

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.