Stuck with URL-rewriting in htaccess with Apache:

2019-06-09 18:00发布

examine my current url rewrites for loading pictures:

// Possibiities: 1 letter char, followed by `=` then the value (numbers/letters)
// w= | h= | c= | q= | f=

IMG-bla/blue_w100.jpg           >>  imgcpu?src=bla/blue.jpg&w=100
IMG-bla/blue_cp.jpg             >>  imgcpu?src=bla/blue.jpg&c=p
IMG-bla/blue_h200.jpg           >>  imgcpu?src=bla/blue.jpg&h=200
IMG-bla/blue_w50_h200_fbw.jpg   >>  imgcpu?src=bla/blue.jpg&w=50&h=200&f=bw 

Essentially I would like to have 1 Ultimate rewrite url, that gives me the freedom to rewrite the below urls with any property, without having to hardcode that exact sequence of rewriting, with one rule per possibility: currently my code is very stupid, inpractical & incomplete its unelegant, thought does work!

// works but utterly unelegant and unpractical as well as incomplete:
RewriteRule ^IMG-(.+)_w(.+).jpg$ imgcpu\.php\?src=$1\.jpg&w=$2 [L]
RewriteRule ^IMG-(.+)_w(.+)_h(.+)_c(.+).jpg$ imgcpu\.php\?src=$1\.jpg&w=$2&h=$3&c=$4 [L]
RewriteRule ^IMG-(.+)_w(.+)_h(.+)_f(.+).jpg$ imgcpu\.php\?src=$1\.jpg&w=$2&h=$3&f=$4 [L]

Now, how can I rewrite this complex rule with the optional property assets? I am in search for that 1 Rule to Rule them all. Your ideas & suggestions are warmly welcome and I deliciously upvote anything that dares to solve this complex puzzle!

1条回答
倾城 Initia
2楼-- · 2019-06-09 18:13

I pondered several methods before reaching this very general solution. Here, I have repeated the same rule several times. It's a sort of divide and conquer strategy where each time the rule is executed, it extracts one parameter.

# Generic rule for extracting one parameter

RewriteRule ^(IMG-[a-z0-9]+/[a-z0-9]+)_([a-z0-9])([a-z0-9]+)([_.].*)$ $1$4?$2=$3 [NC,QSA]
RewriteRule ^(IMG-[a-z0-9]+/[a-z0-9]+)_([a-z0-9])([a-z0-9]+)([_.].*)$ $1$4?$2=$3 [NC,QSA]
RewriteRule ^(IMG-[a-z0-9]+/[a-z0-9]+)_([a-z0-9])([a-z0-9]+)([_.].*)$ $1$4?$2=$3 [NC,QSA]
RewriteRule ^(IMG-[a-z0-9]+/[a-z0-9]+)_([a-z0-9])([a-z0-9]+)([_.].*)$ $1$4?$2=$3 [NC,QSA]
RewriteRule ^(IMG-[a-z0-9]+/[a-z0-9]+)_([a-z0-9])([a-z0-9]+)([_.].*)$ $1$4?$2=$3 [NC,QSA]
RewriteRule ^(IMG-[a-z0-9]+/[a-z0-9]+)_([a-z0-9])([a-z0-9]+)([_.].*)$ $1$4?$2=$3 [NC,QSA]
RewriteRule ^(IMG-[a-z0-9]+/[a-z0-9]+)_([a-z0-9])([a-z0-9]+)([_.].*)$ $1$4?$2=$3 [NC,QSA]
RewriteRule ^(IMG-[a-z0-9]+/[a-z0-9]+)_([a-z0-9])([a-z0-9]+)([_.].*)$ $1$4?$2=$3 [NC,QSA]

# Final rule which does the actual rewrite 
RewriteRule IMG-([a-z0-9]+/[a-z0-9]+\.jpg) /test/index.php?src=$1 [QSA]

Let's break the D&C rule into smaller parts!

^(IMG-[a-z0-9]+/[a-z0-9]+) This part is matched but preserved. This is $1 in the result.

_ A single underscore. This is matched but discarded.

([a-z0-9]) matches a single letter or number (obviously following the undorscore). This is $2 and is assumed to be the parameter name.

([a-z0-9]+) matches one or more letters or numbers. This is $3 in the result and is assumed to be the parameter value.

([_.].*)$ matches a single underscore or period, and everything else up to the end of the request string. This is $4.

So for every step, one parameter is extracted (if there was a match, that is) which is then appended to the query string with the help of QSA. The rest of the request, $1$4 is bundled together and made ready for another round. Rinse and repeat until we're out of parameters or reach the maximum number of rules available.

The final rule matches what should be a path and filename by then. These are the steps:

IMG-bla/blue_w50_h200_fbw.jpg -> IMG-bla/blue_h200_fbw.jpg?w=50 [with QSA]
IMG-bla/blue_h200_fbw.jpg     -> IMG-bla/blue_fbw.jpg?h=200     [with QSA]
IMG-bla/blue_fbw.jpg          -> IMG-bla/blue.jpg?f=bw          [with QSA]

(no more generic rules will match)

IMG-bla/blue.jpg              -> /test/index.php?src=bla/blue.jpg [with QSA]

Grand total after the query strings are appended:

/test/index.php?src=bla/blue.jpg&f=bw&h=200&w=50

Note that I'm making a few assumptions...

  • Everything (apart from underscores, directory slashes and periods) is a letter or number. Might be too strict for you if files or parameters may contain international chars..
  • Parameter names are always one character long, but don't need to be explicitly listed.
  • Images will never be more than one subfolder deep (easy to fix.)

Tell me what you think.

查看更多
登录 后发表回答