Forum Moderators: phranque
1) Does [filename] end in .XYZ?
2) Does [filename].ABC exist?
3) If yes, redirect [filename].XYZ to [filename].ABC. If no, do nothing.
I could just do a blunt redirect with:
RewriteRule ^(.+)\.XYZ$ $1.ABC [R=301,L]
...which works, but I don't like the idea of doing a redirect to another non-existent file, so I'd like to check if it actually exists first.
I know I can accomplish 1) with:
RewriteCond %{REQUEST_FILENAME} ^(.+)\.XYZ$
and I know I can check for files existing with:
RewriteCond %{REQUEST_FILENAME} -f
The problem is, since the filename is dynamic, and we're not checking for the actual requested file, I don't know how to do a RewriteCond -f against the potential new filename.
# If requested .XYZ-filetype resource exists as a .ABC file
RewriteCond %{DOCUMENT_ROOT}/[b]$1[/b].ABC -f
# externally redirect to same-named resource having filetype .ABC
RewriteRule ^([.]+)\.XYZ$ http://www.example.com/$1.ABC [R=301,L]
Because we're only using/retaining *part* of the URL-path for 'exists' testing, we can't use REQUEST_FILENAME. Instead, it is necessary to manually 'construct' the desired new filepath for that requested URL-path, by prepending DOCUMENT_ROOT and adding the replacement filetype to the end.
Jim