%% %% Mod rewrite for Zend Framework %% -module(rewriter). -export([arg_rewrite/1]). -include_lib("yaws/include/yaws_api.hrl"). arg_rewrite(Arg) -> % Decode request path Req = Arg#arg.req, case Req#http_request.path of {abs_path, RawPath} -> case (catch yaws_api:url_decode_q_split(RawPath)) of {'EXIT', _} -> Arg1 = Arg; {Path, _Query} -> % Do not parse file types matching the regexp rule below. % This rule is an exact copy of Apache .htaccess rule. % You can modify it to match your needs. case regexp:first_match(Path, "\.(js|ico|gif|jpg|png|css|html|htm)$") of {match, _Start, _Lenght} -> Arg1 = Arg; _Else -> Elems = string:tokens(Path, "/"), case Elems of [] -> % Rewrite request to index.php NewPath = "/index.php" ++ RawPath, Req1 = Req#http_request{path = {abs_path, NewPath}}, Arg1 = Arg#arg{req = Req1}; [H|T] -> % Check if request already begins with index.php case string:equal(H, "index.php") of true -> % Check for possible index.php duplicates case T of [] -> Arg1 = Arg; [H1|T1] -> case string:equal(H1, "index.php") of true -> % If duplicates found, remove them L = lremove("index.php", [T1]), NewPath = "/index.php/" ++ join(L, "/"), Req1 = Req#http_request{path = {abs_path, NewPath}}, Arg1 = Arg#arg{req = Req1}; false -> Arg1 = Arg end end; false -> % Rewrite request to index.php NewPath = "/index.php" ++ RawPath, Req1 = Req#http_request{path = {abs_path, NewPath}}, Arg1 = Arg#arg{req = Req1} end end end end; _Else -> Arg1 = Arg end, Arg1. % Remove all occurences of Elem from the beginning of the list lremove(_Elem, []) -> []; lremove(Elem, List) -> [H|T] = List, case string:equal(H, "index.php") of true -> lremove(Elem, T); false -> List end. % Join list into a string with elements separated with Sep join([], Sep) -> Sep; join([H|T], Sep) -> H ++ lists:concat([Sep ++ X || X <- T]).