apache - Htaccess recursive file rewrite -
i know if there better way achieve sort or redirection :
# apache configuration options -indexes options +followsymlinks # rewrite engine configuration rewriteengine on rewritebase / # force domain www (302 redirect) rewritecond %{http_host} ^([^\.]+\.[^\.0-9]+)$ rewriterule ^/?(.*)$ http://www.%1/$1 [l,r=302] # request redirection (/module/tld/module/domain/module/sub/data/public/...) rewritecond %{request_uri} !^/index.php rewritecond %{http_host} ^(.+)\.([^\.]+)\.([^\.0-9]+)$ rewritecond %{document_root}/module/%3/module/%2/module/%1/data/public/%{request_uri} -f rewriterule ^/?(.+)$ /module/%3/module/%2/module/%1/data/public/$1 [l] rewritecond %{request_uri} !^/index.php rewritecond %{http_host} ^(.+)\.([^\.]+)\.([^\.0-9]+)$ rewritecond %{document_root}/module/%3/module/%2/module/%1/data/public/%{request_uri}/index.php -f rewriterule ^/?(.+)/?$ /module/%3/module/%2/module/%1/data/public/$1/index.php [l] rewritecond %{request_uri} !^/index.php rewritecond %{http_host} ^(.+)\.([^\.]+)\.([^\.0-9]+)$ rewritecond %{document_root}/module/%3/module/%2/module/%1/data/public/%{request_uri}/index.html -f rewriterule ^/?(.+)/?$ /module/%3/module/%2/module/%1/data/public/$1/index.html [l] rewritecond %{request_uri} !^/index.php rewritecond %{http_host} ^(.+)\.([^\.]+)\.([^\.0-9]+)$ rewritecond %{document_root}/module/%3/module/%2/module/%1/data/public/index.php -f rewriterule ^/?$ /module/%3/module/%2/module/%1/data/public/index.php [l] rewritecond %{request_uri} !^/index.php rewritecond %{http_host} ^(.+)\.([^\.]+)\.([^\.0-9]+)$ rewritecond %{document_root}/module/%3/module/%2/module/%1/data/public/index.html -f rewriterule ^/?$ /module/%3/module/%2/module/%1/data/public/index.html [l] # request redirection (/module/tld/module/domain/data/public/...) rewritecond %{request_uri} !^/index.php rewritecond %{http_host} ^.+\.([^\.]+)\.([^\.0-9]+)$ rewritecond %{document_root}/module/%2/module/%1/data/public/%{request_uri} -f rewriterule ^/?(.+)$ /module/%2/module/%1/data/public/$1 [l] rewritecond %{request_uri} !^/index.php rewritecond %{http_host} ^.+\.([^\.]+)\.([^\.0-9]+)$ rewritecond %{document_root}/module/%2/module/%1/data/public/%{request_uri}/index.php -f rewriterule ^/?(.+)/?$ /module/%2/module/%1/data/public/$1/index.php [l] rewritecond %{request_uri} !^/index.php rewritecond %{http_host} ^.+\.([^\.]+)\.([^\.0-9]+)$ rewritecond %{document_root}/module/%2/module/%1/data/public/%{request_uri}/index.html -f rewriterule ^/?(.+)/?$ /module/%2/module/%1/data/public/$1/index.html [l] rewritecond %{request_uri} !^/index.php rewritecond %{http_host} ^.+\.([^\.]+)\.([^\.0-9]+)$ rewritecond %{document_root}/module/%2/module/%1/data/public/index.php -f rewriterule ^/?$ /module/%2/module/%1/data/public/index.php [l] rewritecond %{request_uri} !^/index.php rewritecond %{http_host} ^.+\.([^\.]+)\.([^\.0-9]+)$ rewritecond %{document_root}/module/%2/module/%1/data/public/index.html -f rewriterule ^/?$ /module/%2/module/%1/data/public/index.html [l] # request redirection (/module/tld/data/public/...) rewritecond %{request_uri} !^/index.php rewritecond %{http_host} ^.+\.([^\.0-9]+)$ rewritecond %{document_root}/module/%1/data/public/%{request_uri} -f rewriterule ^/?(.+)$ /module/%1/data/public/$1 [l] rewritecond %{request_uri} !^/index.php rewritecond %{http_host} ^.+\.([^\.0-9]+)$ rewritecond %{document_root}/module/%1/data/public/%{request_uri}/index.php -f rewriterule ^/?(.+)/?$ /module/%1/data/public/$1/index.php [l] rewritecond %{request_uri} !^/index.php rewritecond %{http_host} ^.+\.([^\.0-9]+)$ rewritecond %{document_root}/module/%1/data/public/%{request_uri}/index.html -f rewriterule ^/?(.+)/?$ /module/%1/data/public/$1/index.html [l] rewritecond %{request_uri} !^/index.php rewritecond %{http_host} ^.+\.([^\.0-9]+)$ rewritecond %{document_root}/module/%1/data/public/index.php -f rewriterule ^/?$ /module/%1/data/public/index.php [l] rewritecond %{request_uri} !^/index.php rewritecond %{http_host} ^.+\.([^\.0-9]+)$ rewritecond %{document_root}/module/%1/data/public/index.html -f rewriterule ^/?$ /module/%1/data/public/index.html [l] # request redirection (/data/public/...) rewritecond %{request_uri} !^/index.php rewritecond %{document_root}/data/public/%{request_uri} -f rewriterule ^/?(.+)$ /data/public/$1 [l] rewritecond %{request_uri} !^/index.php rewritecond %{document_root}/data/public/%{request_uri}/index.php -f rewriterule ^/?(.+)/?$ /data/public/$1/index.php [l] rewritecond %{request_uri} !^/index.php rewritecond %{document_root}/data/public/%{request_uri}/index.html -f rewriterule ^/?(.+)/?$ /data/public/$1/index.html [l] rewritecond %{request_uri} !^/index.php rewritecond %{document_root}/data/public/index.php -f rewriterule ^/?$ /data/public/index.php [l] rewritecond %{request_uri} !^/index.php rewritecond %{document_root}/data/public/index.html -f rewriterule ^/?$ /data/public/index.html [l] # request redirection (main index file) rewritecond %{request_filename} !-f rewriterule ^ /index.php [l,pt]
for exemple, redirect request http://www.testdomain.com/test/file.css
these possibles paths :
/module/com/module/testdomain/module/www/data/public/test/file.css
/module/com/module/testdomain/data/public/test/file.css
/module/com/data/public/test/file.css
/data/public/test/file.css
if none of these paths correct, fall /index.php
. and, request empty uri, try dispatch http://www.testdomain.com/index.html
or http://www.testdomain.com/index.php
. there better way achieve sort of rewriting ?
what trying verbose , bizarre, , bizarre nature caught interest. :-) however, assume must have reasons.
one advantage of being verbose can define want achieve , in 1 .htaccess
file, if aint' broke ... though doing quite few regexp , stat probes in logic chain, nore sure recommend general approach.
also 'options' impact semantics of trying achieve, use recursive nature of rewrite engine advantage, example if /module/tld/module/domain/module/sub/data/public/
uri must interpreted in context, hence like
# if have file match, we're done rewritecond %{request_filename} -f rewriterule ^ - [l] # skip 4 directory rewrites except on first pass rewritecond %{env:redirect_pass} !="" rewriterule ^ - [e:pass=ok, skip=5] rewriterule ^ - [e:pass=ok] # redirect correct subdirectory rewritecond %{http_host} ^(.+)\.([^.]+)\.([^.\d]+)$ rewritecond %{document_root}/module/%3/module/%2/module/%1/data/public/ -d rewriterule ^.*$ /module/%3/module/%2/module/%1/data/public/$0 [l] rewritecond %{http_host} ^.+\.([^.]+)\.([^.\d]+)$ rewritecond %{document_root}/module/%2/module/%1/data/public/ -d rewriterule ^.*$ /module/%2/module/%1/data/public/$0 [l] rewritecond %{http_host} ^.+\.([^.\d]+)$ rewritecond %{document_root}/module/%1/data/public/ -d rewriterule ^.*$ /module/%1/data/public/$0 [l] rewriterule ^.*$ /data/public/$0 [l] # etc ... uris include correct prefix.
note
- a bare
^
matches - a bare
-
in subst string means leave unchanged - don't include leading
/
in per_dir (eg..htaccess
patterns $0
entire match string -- saves using brackets if want whole thing- you don't need escape
.
inside[]
selector. - read on setting , using environment variables. if set variable pass appears redirect_pass after internal redirect.
you need enable rewrite logging , go through logs debug this. have fun.
Comments
Post a Comment