You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@httpd.apache.org by BohwaZ <ph...@bohwaz.net> on 2022/12/10 01:50:15 UTC

[users@httpd] RewriteRule and md5 (or expressions)

Hi everyone,

I'm wondering if it's possible to rewrite the current URL to a MD5 hash
of the request URL.

What I'm trying to do is serve "/cache/[MD5 hash of URL].html" when a
request on "/URL" is done. This is for caching dynamic content: if the
MD5 hash file exists, then serve the static file, if not, the
next RewriteRule will call the script to generate the cache.

I tried this:

<If "-z %{QUERY_STRING} && %{REQUEST_METHOD} =~ /GET|HEAD/ && -f
'%{DOCUMENT_ROOT}/cache/%{md5:REQUEST_URI}.html'">

It works, great.

But then I can't figure out how to actually serve this file.

I tried this:

RewriteEngine On
RewriteRule .* "/var/www/cache/%{md5:REQUEST_URI}.html"

But it doesn't work, as "RewriteRule" doesn't seem to recognize the md5
function. I guess it doesn't handle all expressions, and if my guess is
righ the %{name:...} syntax is related to rewrite maps.

When doing this, Apache is trying to request "/var/www/cache/.html".

Is there another way of doing this?

I thought of setting a variable with SetEnvIfExpr, or with RewriteCond,
but they're not designed for that and I don't think that's possible.

If anyone has an idea, please help me :)

Thank you.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


[users@httpd] Re:

Posted by BohwaZ <ph...@bohwaz.net>.
OK actually that didn't work :(

I'm doing this:

SetEnvIfExpr "%{REQUEST_URI} =~ /^(.+)(?:\?|$)/" CACHE_URI=$1
SetEnvIfExpr "md5(%{ENV:CACHE_URI}) =~ /^(.+)$/" CACHE_MD5_URI=$1

Then:
<If "-f '/home/bohwaz/cache/web/%{ENV:CACHE_MD5_URI}'">

When doing a strace I can see that Apache is doing a stat on
/home/bohwaz/cache/web/ and not on /home/bohwaz/cache/web/MD5_HASH

My understanding of the issue is that the variables defined by
SetEnvIfExpr are not available in <If> conditions.

So I resorted to using RewriteRules:

RewriteCond %{REQUEST_URI} ^(.+)(?:\?|$)
RewriteRule ^ "-" [E=CACHE_URI:%1]
RewriteCond expr "md5(%{ENV:CACHE_URI}) =~ /^(.+)$/"
RewriteRule ^ "-" [E=CACHE_URI_MD5:%1]
RewriteCond %{DOCUMENT_ROOT}/.cache/%{ENV:CACHE_URI_MD5} -f
RewriteRule ^ /.cache/%{ENV:CACHE_URI_MD5} [END]

And it works. But it would have been nice to be able to use <If> as
it's easier to read.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


[users@httpd] Re:

Posted by BohwaZ <ph...@bohwaz.net>.
Thank you Eric! With your help I figured it out:

SetEnvIfExpr
  "'%{DOCUMENT_ROOT}/cache/%{md5:REQUEST_URI}.html' =~ /(.+)/"
  CACHE_PATH=$0

then use it in <If> and RewriteRule, or:

RewriteCond expr "%{md5:REQUEST_URI} =~ /(.+)/"
RewriteRule .* "%{DOCUMENT_ROOT}/cache/%0.html"

Awesome :)

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] RewriteRule and md5 (or expressions)

Posted by Eric Covener <co...@gmail.com>.
On Sat, Dec 10, 2022 at 7:49 AM Eric Covener <co...@gmail.com> wrote:
>
> > I thought of setting a variable with SetEnvIfExpr, or with RewriteCond,
> > but they're not designed for that and I don't think that's possible.
>
> I think the SetEnvIfExpr way is the way to go.

Whoops, I missed in the doc even after checking for it:

> If the TestString has the special value expr, the CondPattern will be treated as an ap_expr. HTTP headers referenced in the expression will be added to the Vary header if the novary flag is not given.

So no need for SetEnvIfExpr. You can get the md5 in the testring and
capture it in the pattern and use it as %1 in your rule




-- 
Eric Covener
covener@gmail.com

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] RewriteRule and md5 (or expressions)

Posted by Eric Covener <co...@gmail.com>.
> I thought of setting a variable with SetEnvIfExpr, or with RewriteCond,
> but they're not designed for that and I don't think that's possible.

I think the SetEnvIfExpr way is the way to go.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


RE: [users@httpd] RewriteRule and md5 (or expressions)

Posted by Sierra Padilla <si...@excitedly.live>.
Hi,
Just checking, are you interested acquiring the list of attendees? Please respond my email. I'm waiting for your response.
We are discussing about Prices And, provide the discount cost.
Thank you.

-----Original Message-----
From: BohwaZ <ph...@bohwaz.net> 
Sent: Saturday, December 10, 2022 4:16 PM
To: users@httpd.apache.org
Subject: Re: [users@httpd] RewriteRule and md5 (or expressions)

Thank you, yes it was also what I thought, but RewriteMap is not available in .htaccess, and this is for an app that is mostly used on mass hosting providers, with no access to the server config.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] RewriteRule and md5 (or expressions)

Posted by BohwaZ <ph...@bohwaz.net>.
Thank you, yes it was also what I thought, but RewriteMap is not
available in .htaccess, and this is for an app that is mostly used on
mass hosting providers, with no access to the server config.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] RewriteRule and md5 (or expressions)

Posted by Frank Gingras <th...@apache.org>.
I would use a plain rewrite map for this case.

On Fri, Dec 9, 2022 at 8:50 PM BohwaZ <ph...@bohwaz.net> wrote:

> Hi everyone,
>
> I'm wondering if it's possible to rewrite the current URL to a MD5 hash
> of the request URL.
>
> What I'm trying to do is serve "/cache/[MD5 hash of URL].html" when a
> request on "/URL" is done. This is for caching dynamic content: if the
> MD5 hash file exists, then serve the static file, if not, the
> next RewriteRule will call the script to generate the cache.
>
> I tried this:
>
> <If "-z %{QUERY_STRING} && %{REQUEST_METHOD} =~ /GET|HEAD/ && -f
> '%{DOCUMENT_ROOT}/cache/%{md5:REQUEST_URI}.html'">
>
> It works, great.
>
> But then I can't figure out how to actually serve this file.
>
> I tried this:
>
> RewriteEngine On
> RewriteRule .* "/var/www/cache/%{md5:REQUEST_URI}.html"
>
> But it doesn't work, as "RewriteRule" doesn't seem to recognize the md5
> function. I guess it doesn't handle all expressions, and if my guess is
> righ the %{name:...} syntax is related to rewrite maps.
>
> When doing this, Apache is trying to request "/var/www/cache/.html".
>
> Is there another way of doing this?
>
> I thought of setting a variable with SetEnvIfExpr, or with RewriteCond,
> but they're not designed for that and I don't think that's possible.
>
> If anyone has an idea, please help me :)
>
> Thank you.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
> For additional commands, e-mail: users-help@httpd.apache.org
>
>