apache - Making RewriteRule portable/relative to the directory of the .htaccess file -
apache - Making RewriteRule portable/relative to the directory of the .htaccess file -
i have next rewriterule makes going domain.com/somepage/?page=3 301 redirected domain.com/somepage/3/:
rewritecond %{the_request} \?page=([0-9]+) [nc] rewriterule ^ /somepage/%1/? [r=301,l] this in .htaccess file in somepage directory.
now, works fine... it's bit of hassle. have dozen pages paginated, in own directory. able re-create .htaccess file each directory , not have worry editing each one. example, want domain.com/someotherpage/?page=3 redirect domain.com/someotherpage/3/, i'd have edit respective .htaccess file , alter somepage part someotherpage.
it causes problems when re-create entire site different server. normally, site placed in root directory. however, when i'm running locally, place entire site in folder called dev. have go through of rewriterules , alter /somepage/%1/ /dev/somepage/%1. this, can imagine, incredibly tedious.
so, there way of changing path redirects relative .htaccess file, rather server's root, can reuse same .htaccess multiple times without changing it, , can move entire site dev directory without needing alter anything?
thanks.
you can rewrite base of operations dynamically captured using separate rule:
rewriteengine on rewritecond $0#%{request_uri} ^([^#]*)#(.*)\1$ rewriterule ^.*$ - [e=base:%2] rewritecond %{the_request} \?page=(\d+) [nc] rewriterule ^ %{env:base}/%1/? [r=301,l] explanation:
you can utilize$0 captured rewriterule in rewritecond because mod_rewrite processes ruleset backwards. starts pattern in rewriterule, , if matches, goes on check 1 or more rewritecond. so can see in rewritecond, lhs (test string) can utilize backreference variables e.g. $1, $2 or %1, %2 etc rhs side i.e. status string cannot use these $1, $2 or %1, %2 variables. inside rhs status part backreference can utilize internal back-references i.e. groups have captured in status itself. denoted \1, \2 etc. in rewritecond first captured grouping ([^#]*). represented internal back-reference `\1. as can mark out rule finding rewritebase dynamically comparing %{request_uri} , $0. illustration of %{request_uri} /directory/foobar.php , illustration of $0 same illustration uri foobar.php. ^([^#]*)#(.*)\1$ putting difference in 2nd captured grouping %2 or \2. here populate %2 or \2 value /directory/ used later in setting env variable %{env:base} i.e. e=base:%2. apache .htaccess mod-rewrite redirect
Comments
Post a Comment