1

I'm using Apache 2.2 and PHP 5.3.8. How should I configure .htaccess to redirect every request do index.php?

I'd like to have URLs like those of stackoverflow or facebook, as: http://mywebsite.com/fancypage where fancypage isn't a directory on the server but just a parameter.

So I thought I needed to redirect every request to a php page, where some code can interpret the $_SERVER['REQUEST_URI'] string. Is this the right way? will $_SERVER['REQUEST_URI'] still be available after the redirection?

Valentino
  • 465
  • 6
  • 17

2 Answers2

5

Try adding the following to the .htaccess file in the root directory of your site.

It will send all requests (except for existing files/dirs) to index.php.

The original request will be available in the request param.

RewriteEngine On
RewriteBase /

#skip existing files or directories
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d
#everything else goes to index.php
RewriteRule ^ index.php?request=%{THE_REQUEST} [L]
Ulrich Palha
  • 9,411
  • 3
  • 25
  • 31
  • thank you, your code fits the bill! anyway i managed to produce a simpler code which fulfils my basic needs and look like this: ` RewriteEngine On RewriteRule ^(products|admin|support|info|etc)/?$ index.php?req=$1 [NC][L]` – Valentino Jan 25 '12 at 00:14
  • i tried to remove the RewriteCond so to match any query.. but i reckon that php include() can't work anymore. is there a workaround? i don't want the user to access the include files.. – Valentino Jan 25 '12 at 19:21
  • @user1168006 I don't understand the question: can you clarify what you mean – Ulrich Palha Jan 25 '12 at 19:24
  • i'd like to match ANY query string and always redirect the users to index.php BUT I need the php function include() to be able to load the files. Maybe there's a way to restrict access to the root subfolders for the users but not the server itself? – Valentino Jan 25 '12 at 19:44
  • @user1168006 The current `RewriteCond` only exclude requests (that originated externally) for existing files or directories. AFAIK, the php include function is not processed within apache, so .htacces rules will not affect it. However, if there is something that is not working, please describe what URL you are testing, the desired outcome and actual incorrect outcome. A new post might be better as it's hard to fit much (and no formattting) in comments. – Ulrich Palha Jan 25 '12 at 20:19
  • `RewriteEngine On` `RewriteBase /` `RewriteRule . index.php?request=%{REQUEST_URI} [NC]` Oh now I get it! the request for .css and .js and image files and php scripts for ajax calls FROM THE CLIENT are all redirected, if I use those rules! I tested the include() function and it works fine. so i considered adding this rule `RewriteCond %{REQUEST_FILENAME} !.*\.(js|css|jpg|gif|png)` but i still the ajax requests were redirected. So I guess I'll simply deny access to the include folder.. – Valentino Jan 26 '12 at 03:05
1

What you are interested in is Apache's url_rewrite function. I suggest you visit these two links and research it a bit:

Kypros
  • 2,997
  • 5
  • 21
  • 27