Router for PHP reqeusts


I want to create a router for my web application, so that all the requests are redirected to index.php and then index.php will route  the requests to the respective scripts. This is just to achieve clean URLs for my web application, so that a URL http://localhost/portal/admin.php?var1=val1&var2=val2 is seen in browser address bar as http://localhost/portal/index.php/admin/var1/val1/var2/val2

If there is any good router already present, kindly let me know the link to it as well as an example of how to use it so that I can implement in my web application accordingly.

Kindly let me know if any more information is required.

Thanks in advance.
Vipin KumarSr. Network EngineerAsked:
Who is Participating?
Julian HansenConnect With a Mentor Commented:
You need to look at URL re-writing - with Apache this uses the mod_rewrite module in conjunction with an .htaccess file. In the latter you specify rules that redirects everything to index.php.

Index.php then looks at the url parameters and uses an internal router to decide where to send those requests

This article may be of some use
Dave BaldwinFixer of ProblemsCommented:
The first thing is to quit using 'localhost' and use at the IP address of the machine.  If you don't you may find that your application ONLY works on 'localhost' and no one else will be able to use it.  The only value of 'localhost' is to run things that you Don't want others to access.
Ray PaseurConnect With a Mentor Commented:
If there is any good router already present
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Vipin KumarSr. Network EngineerAuthor Commented:

I tried laravel... but i have to learn it completely and I have no idea of MVC... so I tried clean URLs using the .htaccess but it was getting very complex with Regex... so thought that if I can achieve that through Router... does building a Router also has to go through the concept of MVC.. is there any way I can achieve clean URLs..
Julian HansenCommented:
Vipin KumarSr. Network EngineerAuthor Commented:

I tried that too.. with the below question i got a solution also, but the problem is that if you check the header then there are multiple redirection happening which is unwanted
Julian HansenCommented:
if you check the header then there are multiple redirection happening which is unwanted
Vipin KumarSr. Network EngineerAuthor Commented:
@Julian.. shall I open a new question, so that you can help me achieve the clean URL stuff??
Julian HansenCommented:
I thought that was what this thread was for. Did you read the article I posted above?
Vipin KumarSr. Network EngineerAuthor Commented:

I have gone through the article that you posted, I just asked for opening a new question is because what I mentioned about .htaccess it was a bit off topic for this question as it was related to router for PHP

Shall I open a new question or you would like to answer the .htaccess stuff in this question only.
Julian HansenCommented:
I think .htaccess is core to the question - it is necessary for any kind of routing solution through a master script (index.php).
Why not take a leaf out of WordPress book and look at their .htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /sitebase/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /sitebase/index.php [L]

# END WordPress

Open in new window

Which says unless the specific file or path exists re-route everything to index.php.
Once you have all friendly URL's flowing to index.php you can build your routing rules internally to decide how to deal with the URL
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.