大网数据广告活动
网站运营与故障问答

Can I use a rewrite rule to send all requests to a single file?

· 52CE 我爱测

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] RewriteR…

问题原文

原帖链接 · Denis Jurkovsek · CC BY-SA 4.0

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.

已采纳解答

原帖链接 · MrWhite · CC BY-SA 4.0

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.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2019-03-29)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。