Connecting Tech Pros Worldwide Forums | Help | Site Map

Virtual URL with PHP?

Newbie
 
Join Date: Jun 2007
Posts: 16
#1: Jun 30 '07
Hi there!

Is it possible, that every request to your website executes only one and the same PHP script everytime?

An example:

www.example.com
www.example.com/thread-183
www.example.com/article-92
www.example.com/start
www.example.com/files/image.jpg

will all execute a single script, let's say "index.php", which somehow has access to the parameters (none, thread-183, article-92, start, files/image.jpg). Is something like this possible? If so, how and how can "index.php" then catch those parameters?

Thanks in advance for any help. :)

mwasif's Avatar
Moderator
 
Join Date: Jul 2006
Location: Pakistan
Posts: 719
#2: Jun 30 '07

re: Virtual URL with PHP?


Apache mod_rewrite does this trick and this is called URL rewriting. You can google on these keywords and you will find a lot of stuff.

After enabling mod_rewrite in Apache, you create rewrite rules to accomodate such URLs. A sample .htaccess rule can be

RewriteEngine On
RewriteRule ^thread-([0-9]+)$ index.php?module=thread&id=$1 [L]
Newbie
 
Join Date: Jun 2007
Posts: 16
#3: Jun 30 '07

re: Virtual URL with PHP?


Quote:

Originally Posted by mwasif

Apache mod_rewrite does this trick and this is called URL rewriting. You can google on these keywords and you will find a lot of stuff.

After enabling mod_rewrite in Apache, you create rewrite rules to accomodate such URLs. A sample .htaccess rule can be

RewriteEngine On
RewriteRule ^thread-([0-9]+)$ index.php?module=thread&id=$1 [L]

Thank you so much! It seems like this is exactly what I need. I'll go google for it. If something I don't understand pops up, I'll just give a shout ;).
volectricity's Avatar
Expert
 
Join Date: Jun 2007
Location: Baltimore
Posts: 587
#4: Jun 30 '07

re: Virtual URL with PHP?


If you are using Apache 1.3 or some other version below v2, be aware that you can't use PCRE, but have to settle for POSIX. If you aren't familiar with POSIX, I wrote a tutorial on parsing clean URLs through PHP if you're interested.
Newbie
 
Join Date: Jun 2007
Posts: 16
#5: Jul 1 '07

re: Virtual URL with PHP?


Quote:

Originally Posted by volectricity

If you are using Apache 1.3 or some other version below v2, be aware that you can't use PCRE, but have to settle for POSIX. If you aren't familiar with POSIX, I wrote a tutorial on parsing clean URLs through PHP if you're interested.

Okay, I'll have a look into that, too. Thank you all so much for your help :)
Reply