Jump to content

Apache PHP funneling


DataRater

Recommended Posts

Apache PHP funnelling

 

I want to make Apache send all URLs to one index.php in the top directory called www say.

 

So all PHP calls will be funneled into index.php. The directory structure so far is...

 

www/index.php

 

and in the Apache .htaccess or httpd.conf we have

 

<IfModule mod_rewrite.c>

  RewriteEngine On

    RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

</IfModule>

 

That is going to send any URL to the index.php with any query string contained in $_GET['url'].

 

Underneath that directory i want to put the js,ico,txt,gif,jpg,png,css filles in directories something like like this

 

www/js

www/pics // this is for the ico, gif, jpg, png files

www/txt

www/css

How do I make the ReWrite do the complete job of funneling into index.php yet allow calls to the other files 'fall through'?

 

 

RewriteEngine On
RewriteRule ^(.*)\.(ico|gif|jpg|png)$ /pics/$1\.$2 [NC,L]
RewriteRule ^(.*)\.js /js/$1.js [NC,L]
RewriteRule ^(.*)\.css /css/$1.css [NC,L]
RewriteRule ^(.*)\.php$ /index.php?url=$1 [QSA]

 

www/ is the web root right?  Like if you went to http://yoursite.com/index.php, it would be showing www/index.php?

 

That would redirect anything.ico or gif or jpg or png to /pics/<file>, and the same with JS and CSS files (to their corrosponding directories).

 

Then, the last statement is what I would consider a catch-all that will forward anything else to index.php?url=<page>.

 

Someone else will probably come up with a better way to do it though since my way isn't very efficient at all.

 

Oh, I should also note, that JS/CSS/Image files with query strings will be redirected to index.php, not their folders.

Hi Corbin,

 

Thanks for that. You've definitely helped me and got some creative juices flowing.

 

For the php part I want something along the lines of digg.    http://digg.com/

 

So if you select 'Technology', you get a URL of http://digg.com/technology and it doesn't show the underlying technology. In my case I want that to go to index.php.

 

Have you any idea how to adjust your solution so it follows the Digg pattern?

 

 

 

 

Hi Corbin and Thorpe,

 

Sorry I didn't answer your question "www/ is the web root right?". Yes it is. :-)

 

Maybe I should explicitely map each psuedo page to the index.php like this. I've used the pages as per digg.

 

  RewriteRule ^technology$  /index.php [L]

  RewriteRule ^gaming$        /index.php [L]

  RewriteRule ^science$      /index.php [L]

But I do want to know what the orginating URL was and if there are any GET parameters so I think I should do this.

 

  RewriteRule ^(technology)$  /index.php?url=$1  [QSA][L]

  RewriteRule ^(gaming)$        /index.php?url=$1  [QSA][L]

  RewriteRule ^(science)$      /index.php?url=$1  [QSA][L]

At least that way only pages I explicitely map get mapped.

 

So this is what I've got now.

 

  RewriteEngine On

  RewriteRule ^(.*)\.(ico|gif|jpg|png)$ /pics/$1\.$2 [NC,L]

  RewriteRule ^(.*)\.js /js/$1.js [NC,L]

  RewriteRule ^(.*)\.css /css/$1.css [NC,L]

  RewriteRule ^(technology)$  /index.php?url=$1  [QSA][L]

  RewriteRule ^(gaming)$        /index.php?url=$1  [QSA][L]

  RewriteRule ^(science)$      /index.php?url=$1  [QSA][L]

Am I on the right track?

 

That should have been

 

RewriteEngine On

  RewriteRule ^(.*)\.(ico|gif|jpg|png)$ /pics/$1\.$2 [NC,L]

  RewriteRule ^(.*)\.js /js/$1.js [NC,L]

  RewriteRule ^(.*)\.css /css/$1.css [NC,L]

  RewriteRule ^(technology)$  /index.php?url=$1  [QSA,L]

  RewriteRule ^(gaming)$        /index.php?url=$1  [QSA,L]

  RewriteRule ^(science)$      /index.php?url=$1  [QSA.L]

I had to put a slash on the ReWrite to get it to work

  RewriteRule ^(technology)$  /index.php?url=$1  [QSA,L]

became

  RewriteRule ^(technology/)$  /index.php?url=$1  [QSA,L]

 

 

This images don't work and I have no understanding why. It definitely looks OK to me. I've checked the file and directory permissions and World has Read

 

  RewriteRule ^(.*)\.(ico|gif|jpg|png)$ /pics/$1\.$2 [NC,L]

 

Any ideas?

 

I cannot read my banner

 

http://space/spacebanner5.jpg

 

I can definitely see into the subdirectory from the browser by doing

 

http://space/pics/

 

it gives me the index which includes the spacebanner5.jpg file

 

The ReWrite role is doing the correct things with $1 and $2 as I've investigated those.

 

So why doesn't the following RewriteRule work???

 

RewriteRule ^(.*)\.(ico|gif|jpg|png)$ /pics/$1\.$2 [NC,L]

Oh dang....  I see the problem now.

 

Let's take an example url http://blah.com/image1.jpg.  That matches the regex so it'll be redirected to http://blah.com/pics/image1.jpg.  The problem is here.  http://blah.com/pics/image1.jpg also matches the regex, so it's redirected to http://blah.com/pics/image1.jpg.  This repeats forever.

 

 

Anyway,

 

RewriteRule ^/(.*)\.(ico|gif|jpg|png)$ /pics/$1\.$2 [NC,L]

 

Will redirect stuff just in the root folder.  For example http://blah.com/image1.jpg would redirect, but http://blah.com/sub_dir/image1.jpg would not.

 

 

It seems the other regexps have the same problem, so you might want to add a / to the front of them.

Hi Corbin,

 

You're a genious!! It is doing a recursive rewrite ( I knew that) and your solution seems to have fixed it. (I didn't know your solution though).

 

The ReWrite was doing

 

http://space/spacebanner5.jpg

http://space/bin/spacebanner5.jpg

http://space/bin/bin/spacebanner5.jpg

http://space/bin/bin/bin/spacebanner5.jpg

...

 

Until Apache figured out it was in a loop.

 

I'm trying to figure out why your solution works!!! duhhhh

 

Thanks very much!!!

 

Steve :-)

 

 

 

Hi Corbin,

 

I rebooted and it isn't doing exactly what I want.

 

Let's recap.

 

I want

http://space/spacebanner5.jpg

to go to

http://space/bin/spacebanner5.jpg

but it is re

 

Your suggested rule

RewriteRule ^/(.*)\.(ico|gif|jpg|png)$ /pics/$1\.$2 [NC,L]

needs the \ removing from the end, like this

RewriteRule ^/(.*)\.(ico|gif|jpg|png)$ /pics/$1.$2 [NC,L]

 

and it won't map

http://space/spacebanner5.jpg

but it will map

http://space/pics/spacebanner5.jpg

 

Which not what i want.

RewriteRule ^/(.*)\.(ico|gif|jpg|png)$ /pics/$1.$2 [NC,L]

Seems to work ok but has the recursion problem so I'm going to try a ReWriteCond

 

back later...

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.