davidandrew
davidandrew

Reputation: 183

Redirect based on whether downloadable file exists

Is it possible to achieve the following via htaccess/mod_rewrite:

If a user attempts to download a file e.g. http://static.example.com/im a downloadable file.pdf and the file does not exist, it redirects to the same named file but with the spaces replaced by underscores, e.g. http://static.example.com/im_a_downloadable_file.pdf

Thanks for any help you can offer

Upvotes: 1

Views: 45

Answers (1)

anubhava
anubhava

Reputation: 785156

Interesting and challenging problem.

Use these 2 rules for this:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule "^(\S*)\s+(\S*\s.*)$" $1_$2 [N]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule "^(\S*)\s(\S*)$" $1_$2 [L]

im a downloadable file.pdf will be internally forwarded to im_a_downloadable_file.pdf only if it doesn't exist.

Upvotes: 1

Related Questions