Router for PHP reqeusts

Hi,

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.
LVL 1
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
https://www.experts-exchange.com/articles/13095/A-simple-PHP-master-Template-framework.html
0
 
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.
0
 
Ray PaseurConnect With a Mentor Commented:
If there is any good router already present
Laravel
0
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:
@Ray,

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..
0
 
Julian HansenCommented:
.htaccess
0
 
Vipin KumarSr. Network EngineerAuthor Commented:
@Julian,

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

https://www.experts-exchange.com/questions/28971545/Generic-Rule-to-Redirect-and-Rewrite-URL-using-htaccess.html
0
 
Julian HansenCommented:
if you check the header then there are multiple redirection happening which is unwanted
Explain
0
 
Vipin KumarSr. Network EngineerAuthor Commented:
@Julian.. shall I open a new question, so that you can help me achieve the clean URL stuff??
0
 
Julian HansenCommented:
I thought that was what this thread was for. Did you read the article I posted above?
0
 
Vipin KumarSr. Network EngineerAuthor Commented:
@Julian,

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.
0
 
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]
</IfModule>

# 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
0
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.