Hi,
I am building an open source mvc. In order to lower server load and to higher the pages load time I’ve places static files (placed in “static_files” dir) and mvc engine in different physical directories (something like “/static_files/” and “/mvc/”).
The idea is to route http requests with help of mod_rewrite.
I am not an mod_rewrite expert. I wrote some Conds and Rules. Works just fine. But imho all that could be done easier and written shorter. Please advise and share your thoughts.
Here is the logic I have come with so far:
- requested index.html?
а) redirect to root of requested dir (пример: /ads/index.html => /ads/) - requested dir not exists?
а) remove trailing slash “/” and redirect (example: ‘/ads/’ = (not found) ? ‘/ads’ : ‘/ads/’ ) - requested file doesn’t exist but there is such dir?
а) redirect to the dir: URI + “/” - requested file exists in static_files dir?
а) yes? - just return that file
б) no? - feed to mvc/index.php?url=requested_uri - GET or POST passed?
a) feed to mvc/index.php?url=requested_uri
Here is the mod_rewite code I have written so far:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^.*/index.html
RewriteRule ^(.*)index.html$ http://test.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^$ mvc/index.php?url= [QSA,L]
#If no directory found - try to cut the trailing slash
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{DOCUMENT_ROOT}/static_files%{REQUEST_URI} !-d
RewriteRule [^/]/$ /$1 [R=301,L]
#If a directory was called without a trailing slash - add it
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{DOCUMENT_ROOT}/static_files%{REQUEST_URI}/ -d
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]
#Make sure static file is loaded if it exists in /static_files/ folder
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{DOCUMENT_ROOT}/static_files%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/static_files%{REQUEST_URI} -d
RewriteRule ^(.*)$ /static_files/$1 [QSA,L]
# If anything is missing in static dir [OR] any parameters passed - feed to dynamic/index.php
RewriteCond %{THE_REQUEST} ^POST [OR]
RewriteCond %{QUERY_STRING} (.+) [OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ mvc/index.php?url=$1 [QSA]
Will be glad to hear your thoughts, comments, suggestions etc.