apache.htaccessserver-administration

Redirect Request to parent directory using .htaccess?


Suppose the user request for the content such as image in www.example.com/content/img/one.jpg.

If the one.jpg is in the directory img then the image is served. But if the one.jpg is not present then user is redirected one directory up to www.example.com/content/one.jpg.

Whether it has the requested file or not.

It must be valid for all content such as images or documents or videos etc.

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ______________________

Thanks


Solution

  • You can use this code in your DOCUMENT_ROOT/.htaccess file:

    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*?/)?[^/]+/(.+)$ $1$2 [L]