apache.htaccessurl-rewritingrulesrewriting

Rewrite rule question / image rewrite htaccess


I'm having this rule which works just fine:

RewriteRule ^([^/-]*)-([^/-]*)-([^/-]*)-([^/-]*)-([^/-]*)-([^/-]*)-([^/-]*)-([^/])*\.jpg$ $1.jpg [QSA]

It means 1-x-x-x-x...jpg will always be in first place of the rule. What I wanted to do is to have is x-x-x-x...1.jpg which means to make it last in this rule but I'm quite lost after hours of trying if this is possible at all.


Solution

  • With your shown samples and attempts, please try following htaccess rules. Please make sure to clear browser cache before testing your URLs. It just has 2 capturing groups, while rewriting mentioning 2nd group later to achieve shown samples name of jpeg.

    RewriteEngine ON
    RewriteRule ^([^-]*)-([^.]*)\.jpg/?$ $2-$1.jpg [QSA,L]
    


    2nd solution: Either use above rules OR use following ones. This is based on your shown attempt which I have fixed here. You need not to mention / in your regex since its not present in .jpg file. Simply we are capturing all values in 8 groups and mentioning 1st group as last to make jpeg as x-x-x-x...1.jpg format mentioned by you in question.

    RewriteEngine ON
    RewriteRule ^([^-]*)-([^-]*)-([^-]*)-([^-]*)-([^-]*)-([^-]*)-([^-]*)-([^.])*\.jpg/?$ $2-$3-$4-$5-$6-$7-$8-$1.jpg [QSA,L]