0

I have an index.php file which I would like to serve different content based on the url structure.

For example. If the url is mysite.com/index.php/folder1 I run a certain query and display the results and if the url is mysite.com/index.php/folder2, run a different query.

The solution also has to work for mysite.com/folder1 etc (ie index.php omitted).

And it would be ideal if the same approach could be extended to mysite/com/folder1/category1 etc. Note, GET variables aren't desired here.

Hard worker
  • 3,916
  • 5
  • 44
  • 73

3 Answers3

2

What you need is a routing component that links url's to classes/functions in your code that do stuff. This component execute certain class/function depending on your request. In this question, different strategies are discussed, although there are some already existing frameworks missing that are interesting:

You could use any of those to do exactly what I mention earlier. For example, using the Symfony Routing Component, you could have your routes (potential urls) in a config file like:

# routes.yml
route1:
    path:     /foo
    defaults: { _controller: 'MyController::fooAction' }

route2:
    path:     /foo/bar
    defaults: { _controller: 'MyController::foobarAction' }

So when the requested uri matches with "/foo", the method fooAction from the class MyController is executed. If you are not sure about how to implement this in your existing codebase, I'd strongly recommend you to read a series of posts about how to use different Symfony components in your code, written by Fabien Potencier. There is a specific post about the routing part.

As for the need to remove "index.php/" from your url, you need to rewrite it. If you are using Apache, you need to add the rewrite rules in your virtual host configuration file, or just create an .htaccess file in your document root. There are hundreds of questions in StackOverflow about this (like this one), and plenty of documentation in the official documentation page.

Community
  • 1
  • 1
Jose Armesto
  • 12,794
  • 8
  • 51
  • 56
0

The best way is probably to assign variables in the browser. e.g.

index.php?folder=1      AND index.php?folder=2

In php then you use:

$folder = $GET_['folder']
if($folder == 1)
{
    //show site 1
}
else if ($folder == 2)
{
    //show site 2
}
else
{
    //error code
}

there are other ways including .htaccess but for someone who does not want to go into that, i found this was the best way

NoLiver92
  • 882
  • 3
  • 15
  • 39
0

Add following RewriteRule in your .htaccess file and put .htaccess file into your webroot

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

Above rule redirects all requests to index.php if there is not file/directory exists on the system.

For example, without above rule if http://example.com/test returns 404 error, then with above redirect rule, this will resolved to index.php. Now, within index.php you can parse the URL and do processing according to your requirements.

Amit
  • 1,365
  • 8
  • 15