【发布时间】:2020-11-13 19:34:06
【问题描述】:
我有一个使用 Apache 的网站。在那个网站上,我可以访问 mywebsite.com/2020/Summer/1,我的 Angular 代码应该处理 2020/Summer/1 部分。它不像目录或任何东西,它只是我为用户格式化数据的一种方式。
我的问题是,在 Apache 中,如果你真的去上面的 URL,你会得到一个The requested URL was not found on this server.。旁注:DocumentRoot 设置正确,因此mywebsite.com 可以正常工作。
如何设置 Apache,以便任何末尾带有日期路径的 URL 都只服务于主页?
ScriptAliasMatch "^/[0-9]{4}/[a-zA-Z]+/[0-9]+" "/home/bitnami/myproject/dist/mywebsite"
我认为这个正则表达式应该可以满足我的需求,但是当我导航到 mywebsite.com/2020/Summer/1 时,我会被重定向到 https://mywebsite.com/2020/Summer/3/index.html/index.html/index.html/index.html/index.html/(keeps going on)。
我该如何解决这个问题?或者 Apache 中是否有其他方法可以做到这一点?
编辑:我的 .conf 文件如下所示,
<VirtualHost _default_:80>
DocumentRoot "/home/bitnami/myproject/dist/mywebsite"
ScriptAliasMatch "^/[0-9]{4}/[a-zA-Z]+/[0-9]+" "/home/bitnami/myproject/dist/mywebsite"
<Directory "/home/bitnami/myproject/dist/mywebsite">
Options Indexes FollowSymLinks
AllowOverride All
<IfVersion < 2.3 >
Order allow,deny
Allow from all
</IfVersion>
<IfVersion >= 2.3 >
Require all granted
</IfVersion>
</Directory>
</VirtualHost>
【问题讨论】:
-
我想你只想使用重写规则,而不是脚本别名匹配。然后,您可以根据 URL 路径适当地填充下拉列表。除了你的正斜杠没有被转义之外,我对你的正则表达式唯一要改变的就是让它更加严格。我会做类似
20\d{2}\/(Summer|Winter|Spring)\/\d{1,2} -
我检查并使用
ScriptAlias而不是ScriptAliasmatch进行了类似的设置。基于此,您的设置看起来不错。纯猜测:您的DocumentRoot和ScriptAliasmatch指向同一个目录,这可能会使 Apache 感到困惑。尝试使用不同的DocumentRoot。 -
我已经解决了这个问题。我错过了
$正则表达式终止符,但我还需要直接指向 index.html 而不仅仅是website文件夹。感谢大家的回复!
标签: regex apache url redirect server