Forum Moderators: phranque
I want to be able to link to extension-free files using content-negotiation without the server automatically redirecting to the actual file with an extension.
I serve my website's images from a folder-based subdomain called "images". (images.example.com really resides at example.com/images/, which redirects to the subdomain).
When I link to files on images.example.com without using the file extension (which is what I want to be able to do), I am redirected to the same file, but with the extension attached. E.g.: images.example.com/photo redirects to images.example.com/photo.jpg.
There is no file at images.example.com/.htaccess. I think the problem may originate at example.com/.htaccess. Here is the information I think might be relevant from that top-level .htaccess file:
AddType image/gif gif AddType image/jpeg jpg AddType image/png png AddType image/svg+xml svg AddType image/tiff tif AddHandler x-httpd-php5 php DirectoryIndex index.php ErrorDocument 403 /error/403.php ErrorDocument 404 /error/404.php Options -MultiViews RewriteEngine on #www is superfluous RewriteCond %{HTTP_HOST} ^www\.example\.com(:[0-9]+)?$ [NC] RewriteRule (.*) http://example.com/$1 [R=301,L] #Hide extensions RewriteCond %{DOCUMENT_ROOT}/$1.png -f RewriteRule ^(([^/]+/)*[^/.]+)/?$ /$1.png [L] RewriteCond %{DOCUMENT_ROOT}/$1.gif -f RewriteRule ^(([^/]+/)*[^/.]+)/?$ /$1.gif [L] RewriteCond %{DOCUMENT_ROOT}/$1.jpg -f RewriteRule ^(([^/]+/)*[^/.]+)/?$ /$1.jpg [L] RewriteCond %{DOCUMENT_ROOT}/$1.php -f RewriteRule ^(([^/]+/)*[^/.]+)/?$ /$1.php [L] #Redirecting to the subdomain Redirect 301 /images http:// images.example.com (There is no space between the "http://" and "images.example.com"; I inserted it here to prevent malformed autolinking.)
If that is the case, then replacing your mod_alias Redirect 301 with a mod_rewrite rule that checks the requested host name and client HTTP request header may cure the problem. As it stands, any path on either domain that starts with /images would be redirected.
RewriteCond %{HTTP_HOST} ^example\.com
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /images/
RewriteRule ^images/(.*)$ http://images.example.com/$1 [R=301,L]
It is also possible that your server is configured with UseCanonicalName on -- If this is the case, you'll need to change the server config to turn it off -- or ask your host to do it.
BTW, the test for THE_REQUEST is intended to prevent a two-step infinite rewrite/redirect loop. It prevents the rule from being applied to requests that have been internally rewritten to /images, allowing it to be applied only for direct client (i.e. browser and 'bot) requests for example.com/images.
Jim