Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Router for PHP reqeusts

Posted on 2016-10-03
12
51 Views
Last Modified: 2016-11-16
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.
0
Comment
Question by:Vipin Kumar
12 Comments
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 41827524
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
 
LVL 55

Accepted Solution

by:
Julian Hansen earned 400 total points
ID: 41827553
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
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 100 total points
ID: 41828148
If there is any good router already present
Laravel
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Author Comment

by:Vipin Kumar
ID: 41828189
@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
 
LVL 55

Expert Comment

by:Julian Hansen
ID: 41828225
.htaccess
0
 
LVL 1

Author Comment

by:Vipin Kumar
ID: 41828228
@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
 
LVL 55

Expert Comment

by:Julian Hansen
ID: 41828243
if you check the header then there are multiple redirection happening which is unwanted
Explain
0
 
LVL 1

Author Comment

by:Vipin Kumar
ID: 41828262
@Julian.. shall I open a new question, so that you can help me achieve the clean URL stuff??
0
 
LVL 55

Expert Comment

by:Julian Hansen
ID: 41828315
I thought that was what this thread was for. Did you read the article I posted above?
0
 
LVL 1

Author Comment

by:Vipin Kumar
ID: 41831909
@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
 
LVL 55

Expert Comment

by:Julian Hansen
ID: 41832009
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Help in good tutorials for PHP, HTML and CSS 6 40
Split wordpress loop 35 56
Number of reviews not counting correctly 2 21
reverse engineer .sql from php files 11 33
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question