I’m trying to create mod_rewrite rules for a modular aplication in PHP. I know how to work things out from PHP but I want to avoid serving files through PHP if possible.

Basically I have a public web root dir and a separate application dir with a modules dir. Inside the modules dir there is a separate dir for each module and inside those there is a public dir too. I want to serve files in the public first if they exists. Else I’d like to serve from the public files of a module. Failing both of above pipe the request to the router script. And if the resource exists but is a dir I’d like to try serving it’s index page. Finally I don’t want to serve autogenerated indexes and I’d like to use external redirects to remove trailing slashes from requests.

I’ll provide examples of what I’m trying to achieve.

Note: in the following examples “/” is mapped to a public apache dir and /path/to/app/ is mapped to a dir that sits outside the apache public dir. “mymodule” conforms to /[a-zA-z][a-zA-z0-9_.-]*/. Also if it’s at all possible I’d like to specify /path/to/app/ as a relative path.

Example 1, without a module name:

If user requests: /myresource.txt
should serve if exists: /myresource.txt
else serve: /index.php

Example 2, with a module name:

If user requests: /mymodule/myresource.txt
should serve if exists: /mymodule/myresource.txt
else serve if exists: /path/to/app/modules/mymodule/public/myresource.html
else serve: /index.php

Example 3, with a module name and a sub-directory:

If user requests: /mymodule/subdir/myresource.txt
should serve if exists: /mymodule/subdir/myresource.txt
else serve if exists: /path/to/app/modules/mymodule/public/subdir/myresource.html
else serve: /index.php

Example 4, with a module name and two sub-directories:

If user requests: /mymodule/subdir1/subdir2/myresource.txt
should serve if exists: /mymodule/subdir1/subdir2/myresource.txt
else serve if exists: /path/to/app/modules/mymodule/public/subdir1/subdir2/myresource.html
else serve: /index.php

Example 5, requesting a directory path:

If user requests: /mymodule/
should serve if exists: /mymodule/index.php
else serve if exists: /mymodule/index.html
else serve if exists: /mymodule/index.htm
else serve if exists: /path/to/app/modules/mymodule/public/index.php
else serve if exists: /path/to/app/modules/mymodule/public/index.html
else serve if exists: /path/to/app/modules/mymodule/public/index.htm
else serve: /index.php

More examples:

/path/to/app/
modules/
first_module/
SomeClass.php

Read more here: Complex framework structure for Apache


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: