views:

20

answers:

3

I've been working on this for a while and have tried a lot of different solutions I've seen on the web and can't seem to get this to work.

I have a site at www.mydomainname.com. The page that I want to handle ALL page requests is www.mydomain.com/index.php. I'd also like to set this up to work for any other domains that I point to this code base (using wildcards would be the way to go for that I think).

So the following URL types (or any other) should automatically go to index.php, while still keeping the original URL structure in the browser address bar:

 www.mydomain.com/
 mydomain.com/
 www.mydomain.com/item/111
 www.mydomain.com/item/itemname/anothervariable/value
 www.mydomain.com/item/itemname/?variable=value

I'm using PHP 5 and a recent version of Apache with mod_rewrite enabled.

Thanks in advance for any help!

A: 

Include this once per .htaccess file:

Options +FollowSymlinks
RewriteEngine on

RewriteRule (.*) index.php

If you need the information from the matched URL you can modify your RewriteRule to match portions of the old URL or just include everything by using the variables $1 and so forth. If for instance you wanted to get the item number passed in quickly to index.php, you could use this rule:

RewriteRule item/(.*)$ index.php?item=$1
spig
A: 

Simple:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} !\.(jpg|gif|ico|png|bmp|css|js)$

RewriteRule .* index.php
Andrei Serdeliuc
This does indeed work, but I did forget one thing, sorry... internal links such as those to CSS files, script files, images, and included PHP files (in specific directories) etc, would need to still function properly -- they don't seem to be after I implemented this solution. I'm sorry for not realizing to mention this.
Gary
There you go, updated :)
Andrei Serdeliuc
That did it... thanks very much!
Gary
If you check this one : http://stackoverflow.com/questions/3470040/help-writing-an-htaccess/3470102#3470102 you do not need to add exceptions based on file extension. It checks if the file or directory "physically" exists on the server. If not, it'll redirect to your index.php. In this one, if you add a .swf or .pdf file, you will need to modify your .htaccess file.
Gabriel
Thanks Gabriel, I will check that one out also. :)
Gary
Gabriel, doesn't `RewriteCond %{REQUEST_FILENAME} !-f` do that already?
Andrei Serdeliuc
A: 

You could use the follow RewriteRule

RewriteEngine On
RewriteRule ^(.*)$ /index.php?originalUrl=$1

Untested, but it should work. You will then also have the original URL available in the 'originalUrl' GET parameter for further parsing.

Seidr