1

I have been working on this for a while but I'm not finding exactly what I am looking for. I am writing a webapp to let my users create and publish pieces of HTML content in a domain and URL folder structure of their choosing. All of the content and requested URL structures are stored in a database. I have all of the code in my index.php (in the root folder) to access the database content, and based on the server name (and hopefully folder structure) will pick out the proper content from the DB and display it to the end-users browser. So my situation looks like this:

www.test.com/index.php?id=123234345

... will display the proper page, but I want my users to be able to define a unique "page name" instead of using the numeric index (also I want to hide the /index.php part) so what I would like the end-user to see is:

www.test.com/arbitrary-unique-keyword/keyword2/keyword3

which will invoke the index.php page in the root folder. Then I will use the PHP $_SERVER['PATH_INFO'] variable to match the requested folder structure up with the proper content in my database and display that. All the material I have found so far expects me to hard code parts of the folder structure into the rules.... but I think I want something simpler (perhaps).

So the question in a nutshell:

How do I use mod_rewrite to allow all "non-existent" folder paths be passed through to a main index.php residing in the root folder? (For all paths that DO exist, like for calls to images... I want those to succeed and not be directed to the index.php obviously)

Thanks everyone, please let me know if I can clear anything up.

Jeff Atwood
  • 13,104
  • 20
  • 75
  • 92

1 Answers1

1

You can accomplish that with RewriteCond using the -f condition, which tests for file existence, e.g.:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php/$1

However, the situation can be more complex if there are multiple rewrite conditions or Alias/ScriptAlias directives. In this case the server may not be able to provide REQUEST_FILENAME. A safer approach, but one that can be wrong for complex set-ups, is to use directly DOCUMENT_ROOT, i.e.

RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_URI} !-f
RewriteRule ^(.*)$ /index.php/$1
Dan Andreatta
  • 5,454
  • 2
  • 24
  • 14