apache - Use variables to simplify mod_rewrite rules in .htaccess file -
i have inherited .htaccess file extremely complex set of mod_rewrite rules, there 100 lines lot of duplication this:
rewritecond %{request_uri} !^/(test|dev|((current|archive|all|2x|19|20|21|22|23|24|25|26|27)/([a-z]{2}|pt_br)))/favicon.ico rewritecond %{request_uri} !^/(test|dev|((current|archive|all|2x|19|20|21|22|23|24|25|26|27)/([a-z]{2}|pt_br)))/robots.txt rewriterule ^(test|dev|((current|archive|all|2x|19|20|21|22|23|24|25|26|27)/([a-z]{2}|pt_br)))/(.*) /index.php?title=$5 [l,qsa,s=1]
(note rules have slight differences , aren't same, there constituent parts consistent between them.)
i use variables make these rules more maintainable, possible? long lines of pseudo-example:
# define variables versions=(current|archive|all|2x|19|20|21|22|23|24|25|26|27) languages=(current|archive|all|2x|19|20|21|22|23|24|25|26|27) # body of rules rewritecond %{request_uri} !^/(test|dev|($versions/$languages))/favicon.ico rewritecond %{request_uri} !^/(test|dev|($versions/$languages))/robots.txt rewriterule ^(test|dev|($versions/$languages)/(.*) /index.php?title=$5 [l,qsa,s=1]
well cannot store partial regex in variables this.
as workaround can set env variables if these conditions true:
rewriterule (current|archive|all|2x|19|20|21|22|23|24|25|26|27) - [e=version:$1] rewriterule (current|archive|all|2x|19|20|21|22|23|24|25|26|27) - [e=lang:$1]
once done can use these environment variables in rules in conditions:
rewritecond %{env:version} .+
or
rewritecond %{env:lang} .+
Comments
Post a Comment