Forum Moderators: phranque

Message Too Old, No Replies

Removing references to file extensions in a subdomain

images.example.com/photo redirects to images.example.com/photo.jpg

         

Lucas

11:05 pm on Sep 1, 2008 (gmt 0)

10+ Year Member



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.)

jdMorgan

11:36 pm on Sep 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Based on the presence of the 'extensionless-image-file mapping' in this code, I'd guess that the main domain and the images subdomain both 'map' to the same path, so that code in this .htaccess file is applied to requests for images.example.com as well as for example.com requests.

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]

However, it's not clear where the "mapping" of images.example.com to example.com/images is taking place, so if this .htaccess file is NOT shared between both domains, then you'll have to look into your server config files (e.g. httpd.conf) because the problem is likely there. If you do not have server-config-level access, then investigate your "control panel" settings -- It's possible that your could cause (and fix) the problem there.

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