Jump to content

How to include uri content after route traffic to controller.php ?


Recommended Posts

Hi;

I have succesfully managed to route all the traffic to the file controller.php using this command line of .htaccess :

RewriteEngine On
RewriteRule (.*) /var/www/html/site/controller.php [L,QSA]

Now how to tell the controller.php to process the url that was passed. For example i have tried that:

 

controller.php :

<?php
    include( $_SERVER["REQUEST_URI"] );
?>

but the controller.php didn't do anything i keep getting a blank page when i browse any webpage such as site/index.php

so how to tell controller.php to echo the content of the request ? because the user shouldn't notice any change as if there is no controller.php.

 

Thank you.

You have to look at the REQUEST_URI and decide what to do. It's going to be (the path part of) the URL, not a filename. And it's even going to have the query string in there.

 

Or you can modify your .htaccess to say

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !=/var/www/html/site/controller.php
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule (.*) /var/www/html/site/controller.php?filename=%{REQUEST_FILENAME} [L,QSA]
Then $_GET["filename"] will be the originally-requested file. Except now, instead of figuring out what file to show, you have to validate the filename - otherwise I could go to

http://www.yoursite.com/controller.php?filename=anything I want
and get the contents of whatever file I choose.

Thank you very mush, your idea works perfectly. Just have a little issue i would like you to help me with please. When i move the controller.php file into a sub directory such as ctrl, the paths of all the files become:

 

The controller full path : /var/www/html/site/ctrl/controller.php

The .htaccess full path : /var/www/html/site/.htaccess

The index.php full path : /var/www/html/site/index.php

 

And this is the content of my controller.php

<?php
include( $_GET['filename'] );
?>

The issue in this case is that /var/www/html/site/index.php which represent http://www.yoursite.com/index.php render html content okay but the css and javascript files paths contains wrong paths so they don't render for example i get :

<link rel="stylesheet" href="/site/ctrl/templates/system/css/system.css" type="text/css" />

instead of

<link rel="stylesheet" href="/site/templates/system/css/system.css" type="text/css" />

The same for other css and javascript files so what is the solution in this case because i have no clue ? being in .htaccess or in controller.php.

 

Thank you

Edited by Dareros

Okay, i am using Joomla. So can i grab the requested page content and do preg_replace all occurences of /ctrl from links. Also is your .htaccess trick will alow passing QUERY_STRING such as a=alpha&b=beta to the controller.php because in your example $_GET['filename'] contain the full path to the requested physical script and so won' contain any query string on it also how does controller.php will process post requests ?

Thank you

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.