Marcin
Marcin

Reputation: 5589

htaccess - RewriteRule for static files

how to rewrite urls for images and other static files to specific folder, i.e. I am using this so far:

RewriteRule \.(woff|ttf|svg|js|ico|gif|jpg|png|css|htc|xml|txt)$ /MyFolder/$1

but its not working,

please help.

------------------- update ---------------------

ok so now I have:

RewriteCond %{HTTP_HOST} ^(www.)?exmpale.com$
RewriteCond %{REQUEST_URI} !^/exmpale.com/
RewriteRule ^(.*\.(woff|ttf|svg|js|ico|gif|jpg|png|css|htc|xml|txt))$ /exmpale.com/$1

RewriteCond %{HTTP_HOST} ^(www.)?exmpale.com$
RewriteRule !\.(woff|ttf|svg|js|ico|gif|jpg|png|css|htc|xml|txt)$ /exmpale.com/index.php

so images redirected to exmpale folders work but now I would like to rewrite everything else to index.php, above solution is not working so far.

cheers, /Marcin

Upvotes: 0

Views: 6946

Answers (2)

Laurence Gonsalves
Laurence Gonsalves

Reputation: 143284

Maybe this is what you want:

RewriteRule ^(.*\.(woff|ttf|svg|js|ico|gif|jpg|png|css|htc|xml|txt))$ /MyFolder/$1

or perhaps this:

RewriteRule ([^/]*\.(woff|ttf|svg|js|ico|gif|jpg|png|css|htc|xml|txt))$ /MyFolder/$1

If neither of those is what you're looking for please add details with some examples of your desired inputs/outputs.

As for your update, you probably want to use the L flag to stop rule processing when this rule matches. eg:

RewriteCond %{HTTP_HOST} ^(www.)?exmpale.com$
RewriteCond %{REQUEST_URI} !^/exmpale.com/

# The next line is long. Scroll to the end!
RewriteRule ^(.*\.(woff|ttf|svg|js|ico|gif|jpg|png|css|htc|xml|txt))$ /exmpale.com/$1 [L]

RewriteCond %{HTTP_HOST} ^(www.)?exmpale.com$
RewriteRule .* /exmpale.com/index.php

Upvotes: 4

Jason McCreary
Jason McCreary

Reputation: 73011

You are not capturing the initial request. Try the following:

RewriteRule (.*)\.(woff|ttf|svg|js|ico|gif|jpg|png|css|htc|xml|txt)$ /MyFolder/$1.$2

This will get anything that ends in the extension specified and redirect under /MyFolder/ For example: /something/image.jpg will now go to /MyFolder/something/image.jpg

Note: There may be better ways to do this by adding conditions to check for file type.

Upvotes: 1

Related Questions