Web Hosting Talk







View Full Version : Force all requests through file


bumfilter
08-29-2005, 10:30 PM
Hello,

I'm trying to setup the following;

I want all requests to my website, e.g.

http://domain.com/contact
http://domain.com/contact/
http://domain.com/file.zip

etc, to be served by one file. I can do this fine using:

<Files index>
ForceType application/x-httpd-php
</Files>

which would allow:

http://domain.com/index/contact
http://domain.com/index/contact/
http://domain.com/index/file.zip

but I want it all to go through from the root.

<Files index>
ForceType application/x-httpd-php
</Files>
DirectoryIndex index

Allows:

http://domain.com/?contact
http://domain.com/?contact/
http://domain.com/?file.zip

but I'm trying to get rid of the ?'s.

It's very hard to Google for answers for this, hence my post! I'm using a script to decide what to serve based on the URL, rather than having many directories etc... So is it possible to force Apache to serve all requests through one script like this?

Thanks if you can help!

Dan L
08-29-2005, 10:39 PM
Google 'mod_rewrite' :)

bumfilter
08-30-2005, 07:50 AM
Any idea what to use? I thought mod_rewrite was mainly for URL re-writing etc. Also, would using this cause lots of re-directs?

I could have used what php.net uses but I don't want to send broswers 404's first, then send them on their way, I would rather it was just straight there!

maxymizer
08-30-2005, 08:00 AM
RewriteEngine On
RewriteRule ^.*$ /index.php [L]

That would pretty much redirect ANY request to your index.php (assuming you're using mod_rewrite with httpd.conf instead .htaccess).

Note that even images, css, txt - all would go to index.php.

To leave some stuff out of the rewrite, you'd have to use something like

RewriteRule !\.(jpg|gif|ico|txt)$ /index.php [L]

That would leave everything ending with jpg, gif, ico, and txt out of the rewrite process.

Hope that helps.

bumfilter
08-30-2005, 08:07 AM
Ah, I need to use this on a shared server environment.

You've given me something to go with so I'll look into mod_rewrite a bit further.

maxymizer
08-30-2005, 08:18 AM
If you have no access to httpd.conf then you HAVE to go with .htaccess.
But the module acts kinda differently when using .htaccess.

Anyway, if you're using .htaccess, do the following:

RewriteEngine On
RewriteBase /absolute/path/to/document/root/
RewriteRule !\.(jpg|gif|ico|txt)$ /index.php [L]

For this example RewriteBase might not be necessary, but if there are some rewriterules used that include directories then regexps need to be changed a bit.