Rewriting missing file to generator script - review?

Discuss practical ways rearrange URLs using mod_rewrite.

Rewriting missing file to generator script - review?

Postby mothmenace » Sun Sep 21, 2008 4:18 am

Hello, I've been working on a script to pass requests for missing files (namely resized thumbnail images) to a generator script. Generated files are created/stored with the query string appended to the filename. I would greatly appreciate any feedback on the script below, ie how efficient it is, or if there's room for improvement!

Many thanks

Code: Select all
RewriteEngine On


# This simulates query params passed to img files.
# looks for a pre-created image. Any missing file is regenerated.


# if query present and file exists, redirect to correct filename (with no query)
RewriteCond     %{QUERY_STRING}   !^$
RewriteCond     %{REQUEST_FILENAME}-%{QUERY_STRING} -f
RewriteRule   ^(.*)$   %{REQUEST_FILENAME}-%{QUERY_STRING}?  [L]


# if query string is present and file does not exist, generate
# rewriteCond here == only generating files with queries, as opposed to all missing files
RewriteCond     %{QUERY_STRING}   !^$
RewriteRule     ^(.*/)*(.*)$   /path/to/generator.php?%{QUERY_STRING}&file=$2&dir=$1  [L]


# if made it here == serve as usual


Would it be possible to combine to two blocks into one by skipping a rewrite? As the script is effectively runs twice (because it redirects to same folder), does the second time count as a sub-request, which can be skipped with a [NS] flag?
mothmenace
 
Posts: 2
Joined: Sun Sep 21, 2008 4:10 am

Postby richardk » Sun Sep 21, 2008 11:43 am

You can't combine the two.

Explain where it runs it twice.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby mothmenace » Sun Sep 21, 2008 12:09 pm

Hiya,

Well after the first rewrite rule, the rewrite ends, but it is redirected back to the same folder (just looking for a different file), and the script runs again. eg:

input: file.jpg?width=100
rewritten to: file.jpg-width=100

as it's looking for the 2nd time in the same folder, the .htaccess is applied again I believe.
mothmenace
 
Posts: 2
Joined: Sun Sep 21, 2008 4:10 am

Postby richardk » Tue Sep 23, 2008 11:31 am

You can't stop that.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: Google [Bot] and 8 guests

cron