2

Can I make a single regular expression for all my RewriteRules? I'm rewriting everything to a single file and this this acts like a front-controller.

My example:

RewriteRule ^de/kontakt/anfahrt$ kontakt_anfahrt.html [NC]
RewriteRule ^en/contact/approach$ kontakt_anfahrt.html [NC]

I don't want to make for every new file a new rule.

  • 2
    "...new file a new rule" - this depends entirely on the pattern that links these files (ie. URLs). The two rules/URLs you've posted appear to be entirely different, with no discernable pattern - so combining these two specific rules (which you could do) doesn't really benefit you. Please explain more about what it is your are trying to do. Are you rewriting everything to this single file (and this file acts like a front-controller)? – MrWhite Mar 29 at 13:09
  • 1
    Yeah, im rewriting evertything to a single file and this this acts like a front-controller. – Denis Jurkovsek Mar 29 at 13:33
1
RewriteRule ^de/kontakt/anfahrt$ kontakt_anfahrt.html [NC]
RewriteRule ^en/contact/approach$ kontakt_anfahrt.html [NC]

im rewriting evertything to a single file and this this acts like a front-controller.

The above two mod_rewrite rules rewrite the specific URLs /de/kontakt/anfahrt and /en/contact/approach to your "front-controller". To rewrite everything you just need to generalise the regex to match anything. For example:

RewriteRule ^ kontakt_anfahrt.html [L]

The regex ^ simply matches anything, you could also use .? (or .* - less efficient) here.

(No need for the NC flag, since you are matching everything. But the NC flag was possibly a bad idea in your original directive as it was promoting duplicate content.)

However, the above probably rewrites too much, as it will also rewrite your static resources (CSS, JS and images), unless these are managed by a different host. So, you could modify the above to rewrite everything, excluding .css, .js and images (.jpg and .png in this example):

RewriteRule !\.(css|js|jpg|png)$ kontakt_anfahrt.html [L]

The above rewrites everything providing the requested URL does not end in one of the file extensions mentioned.

However, even this might not be enough if you are serving a multitude of different file types and/or you have other files that need to be served independently of your front-controller (and/or you wish your front-controller to handle more). Then you can exclude any request that maps to a physical file. For example:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !\.(css|js|jpg|png)$ kontakt_anfahrt.html [L]

If you need to also exclude directories then add another condition:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !\.(css|js|jpg|png)$ kontakt_anfahrt.html [L]

So, the above states, if the requested URL does not end in one of the stated file extensions (ie. is a static resource) and does not map to a physical file and does not map to a directory then internally rewrite the request to kontakt_anfahrt.html in the same directory (depending on the value of the RewriteBase directive - if any).


However, depending on your requirements, instead of using mod_rewrite/regex above you could simply use the FallbackResource directive:

FallbackResource /kontakt_anfahrt.html

This basically does the same thing. If Apache determines that the requested URL would otherwise result in a 404 then it routes the request to /kontakt_anfahrt.html.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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