I need to redirect URLs of all .dmg files at domain's top level to specific folder.

By way of example:

http://example.com/file.dmg to http://example.com/downloads/file.dmg

I can't hard code the domain name because it's a temporary domain which will be switched once the new website is completed.

I don't understand why the following doesn't work:

RewriteRule ^.*\.com/(.*)\.dmg$ /downloads/$1.dmg [R=301,L,NC] 

I get "page not found" error. A .htaccess tester website also reports failure.

What am I missing?

It's a WordPress website if it makes any difference.

  • Where in your .htaccess file did you place that rewrite rule? Did you put it at the top, above the WordPress rules, or at the bottom? Commented Aug 6, 2020 at 1:05
  • 1
    Why the test for the .com in the rewrite rule when your example path (file.dmg) doesn't have it? Commented Aug 6, 2020 at 1:06
  • I'm using the Redirection WordPress plug-in with its "Apache group" option. With this option, it writes all rewrite rules to the .htaccess file. They are at the top of the .htaccess file.
    – Leo Braun
    Commented Aug 7, 2020 at 6:43
  • I test for .com because I need to redirect files from the top level of the domain. As far as I understand, if I use /file.dmg it will also apply to the files in the /downloads folder. I started with this and ran into "too many redirects" error. When it comes to 301 redirect and advanced regex I'm an amateur.
    – Leo Braun
    Commented Aug 7, 2020 at 6:49

1 Answer 1


A RewriteRule only matches the file path. You can't include part of the domain name in the rewrite rule because the rule will never match it.

To match something at the start of the path, use the "starts with" regex operator: ^.

To ensure that you are not matching anything in sub-directories, make sure not to match any slashes in your pattern. [^/]+ instead of .* means "at least one character that isn't a slash" rather than "zero or more characters". The caret inside the square bracket is the negation of a character set.

Your final rewrite rule should be:

RewriteRule ^/?([^/]+\.dmg)$ /downloads/$1 [R=301,L,NC]
  • ^/?: starts with an optional slash
  • (...): parenthesis around the file name (including ".dmg") which becomes $1 in the replacement
  • [^/]+: At least one character other than slashes
  • \.: A literal period
  • $: "ends with", ensure there is nothing after the file name

Make sure this rewrite rule is at the top of your .htaccess file so that it takes precedence over other rewrite rules including the default wordpress rules.

  • It works! Thanks a lot for the detailed explanation, Stephen. One note:
    – Leo Braun
    Commented Aug 8, 2020 at 2:29
  • One note: for some reason, Redirection plugin in WP removes the first slash when writing to .htaccess file: ^?... Which naturally resulted in my site not loading at all. I added another slash in Redirection, which made Redirection write the correct expression: ^//?... is written as ^/?... Is it a bug that I should report to Redirection developer? Or is it normal - and people experienced in regex would know to add the second slash in this case?
    – Leo Braun
    Commented Aug 8, 2020 at 2:37
  • When a RewriteRule is used in .htaccess, the starting slash is not present on the path. When the same rule is used in an Apache configuration file, a starting slash is required. I usually use an optional slash /? (the question mark is zero or one of the preceding) so that rules work in either place. It sounds like the plugin has some logic to convert conf rules to .htaccess rules by removing the slash. In this case it should also be removing the question mark or it completely breaks the rule. Commented Aug 8, 2020 at 9:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.