0

This is my first question:

I have created a URL Shortner Service in PHP.It works completely without any problem but there was a problem:

Someone who wants to access his url should type : MyDomain.com/go.php?u=key Here i tried to remove .php extention by configuring apache and worked.Now it is like that: MyDomain.com/go?u=key

but some services such TinyUrl.com works like that: TinyURL.com/key !!!!

How can i get this in php?

thanks a lot.

3 Answers3

1

You basicly use mod_rewrite.

With mod_rewrite you can say that all requests which are like

www.example.com/[A-Za-z1-9]

are redirected to:

www.example.com/shorturl.php?key=$1

While $1 is the extracted variable from the requested URL.

There is no proper way to do it with pure PHP.

The rewrite rule could look like this:

RewriteRule ^([A-Za-z1-9]*)$ shorturl.php?key=$1 [L]

I would exclude files which really exists from the rewriting, use for this RewriteCond.

This could be done like here:

# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
# If the request is not for a valid link
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([A-Za-z1-9]*)$ shorturl.php?key=$1 [L]

(Source: anubhava at RewriteCond to skip rule if file or directory exists)

Community
  • 1
  • 1
D. Schalla
  • 655
  • 4
  • 9
  • Isn't it like this? [A-Z-a-z-1-9] (Are you sure of proper place of dashes?) – user3631262 May 13 '14 at 07:15
  • I am sure about the dashes, I tried it at even at http://htaccess.madewithlove.be/ – D. Schalla May 13 '14 at 07:16
  • Just make sure you place a "RewriteEngine on" on prior to the RewriteCond. – D. Schalla May 13 '14 at 07:18
  • Isn't it needed to check if mod_rewrite.c exists? – user3631262 May 13 '14 at 07:28
  • Of course you can and should do this, this was not a full solution, I rather focused on explaining how to do it and what it does. You can expect on the most server that they have mod_rewrite.c running, but still a check if it is active should be done. – D. Schalla May 13 '14 at 07:35
  • @user3631262 tell me... how could somebody think that it should be `[A-Z-a-z-1-9]`? what is the idea here? you don't seem to get it at all... you can't pass your life on copy-paste... it won't work! – Flash Thunder May 13 '14 at 07:37
0

create .htaccess file to redirect any request to your domain to one file lets say go.php

so in .htacess do like this:

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ go.php?key=$1 [PT,L]

</IfModule>

This redirection any request like mydomain.com/userkey to mydomain.com/go.php?key=userkey

now in your index.php you can do your redirection login.

<?php 

$key = $_GET['key'];

// your logic here. 

?>
-2

This ref will solve your problem.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9]+)/?$ redirect.php?c=$1 [L] 
</IfModule>
Ahtesham
  • 1
  • 2
  • 1
    Ahtesham, a link to a solution is welcome, but please ensure your answer is useful without it: [add context around the link](//meta.stackexchange.com/a/8259) so your fellow users will have some idea what it is and why it is there, then quote the most relevant part of the page you are linking to in case the target page is unavailable. [Answers that are little more than a link may be deleted.](/help/deleted-answers) – Ethan Nov 27 '22 at 22:33
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 01 '22 at 06:54