Regex Rewrite Filename
Having trouble rewriting the name of a flash file:
/flash/shell.1257347618.swf (the numbers are a changing timestamp, the idea is to bypass browser caching).
My .htaccess looks like this:
#Use mod.rewrite
RewriteEngine On
#Cache files which are autoversion开发者_高级运维ed
ExpiresActive On
<FilesMatch "\.(swf)$">
ExpiresDefault "access plus 10 years"
</FilesMatch>
#Remove autoversion timestamp
RewriteRule ^(swf)/(.+)\.([0-9]+)\.(swf)$ $1/$2.$4 [L]
This was passed to me from a third party and it's not matching properly and I'm just no good at regex. Can anyone out there help me out?
The URL I'm testing with is similar to:
http://dev.somedomain.domain.com/flash/shell.1257347618.swf
And I've installed the .htaccess file to the flash directory.
Whatever your folder name and filename the rule:
RewriteRule ^([^/]+)/([^.]+)\.[0-9]+\.swf$ $1/$2.swf [L]
will convert somefolder/file.329873.swf
into somefolder/file.swf
.
If you need to allow files with subfolders like topfolder/somefolder/file.329873.swf
and want to keep all folder hierarchy use:
RewriteRule ^(.+)/([^.]+)\.[0-9]+\.swf$ $1/$2.swf [L]
If you may have names without the numeric part, use
RewriteRule ^([^/]+)/([^.]+)(\.[0-9]+)?\.swf$ $1/$2.swf [L]
Final answer was in the comments. The problem was that the .htaccess was in the same folder as the .swf files so we needed to remove the folder sectio nin the regex like the following:
RewriteRule ^(.+)\.[0-9]+\.swf$ $1.swf [L]
You use flash/ but the rule assumes swf/, change to:
RewriteRule ^(flash)/(.+)\.([0-9]+)\.(swf)$ $1/$2.$4 [L]
If all files are in the folderName folder and have the extension .swf, you can use the following:
RewriteRule ^folderName/([^.]+)\.\d+\.swf$ folderName/$1.swf [L]
Updated to match different file types in the same folder:
RewriteRule ^folderName/([^.]+)\.\d+\.(\w+)$ folderName/$1.$2 [L]
精彩评论