Mailing List Archive

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
Re: RewriteRule and md5 (or expressions) [ In reply to ]
I would use a plain rewrite map for this case.

On Fri, Dec 9, 2022 at 8:50 PM BohwaZ <php@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
>
>
Re: RewriteRule and md5 (or expressions) [ In reply to ]
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: RewriteRule and md5 (or expressions) [ In reply to ]
> 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: RewriteRule and md5 (or expressions) [ In reply to ]
On Sat, Dec 10, 2022 at 7:49 AM Eric Covener <covener@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: RewriteRule and md5 (or expressions) [ In reply to ]
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 <php@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