archvist
archvist

Reputation: 722

Remove directory from image path htaccess

I'm trying to remove the directory /images/ from an image path. When an image is submitted via a form it's stored in /images/ I created a htaccess rule to remove the images directory but I'm not sure it works with images, as I get a 404.

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /

RewriteRule ^images/(.*)$ /$1 [L,NC,R]

Do I need to create a php page to serve the image and remove that page using htaccess? The goal is to have domain.com/img.png

Thanks

Upvotes: 2

Views: 640

Answers (1)

anubhava
anubhava

Reputation: 785761

This should work for you:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /

# deny direct access to /images/
RewriteCond %{THE_REQUEST} /images/ [NC]
RewriteRule ^ - [F]

# internal rewrite to add /images/ silently
RewriteCond %{REQUEST_URI} !/images/ [NC]
RewriteRule \.(?:jpe?g|gif|bmp|png|tiff|ico)$ images%{REQUEST_URI} [L,NC]

Upvotes: 1

Related Questions