Search code examples
apache.htaccessmod-rewriteurl-rewritingfriendly-url

How to remove md5 from the filename in the requested URL for the file name without md5 using apache mod_rewrite?


Example:

This => URL request ./any_folder/main.d25a1b054a0b0fbeb3def5a0ff50d01e.min.js

For this => URL request ./any_folder/main.min.js

My htaccess:

RewriteEngine On

# Remove md5 from asset files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+).([a-fA-F0-9]{32}).(.+)?(js|css)$ $1.$3 [L]

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]

# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

The above configuration does not work.


Solution

  • Have your htaccess rules file following manner. Please make sure to clear your browser cache before testing your URLs.

    RewriteEngine On
    
    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
        
    # Remove md5 from asset files
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    ##RewriteRule ^([^.]*)\.(?:[[:alnum:]]{32})\.([^.]*)\.(js|css)/?$ $1.$2.$3 [L]
    ##Since following has worked for OP, so adding it here, commenting above.
    RewriteRule ^(.+).([a-fA-F0-9]{32}).(.+)?(js|css)$ $1.$4 [L]
    
    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+)/$ $1 [L,R=301]
    
    # Send Requests To Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]