【问题标题】:Apache - Using ScriptAliasMatch to serve the home page for different URL pathsApache - 使用 ScriptAliasMatch 为不同的 URL 路径提供主页
【发布时间】: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 进行了类似的设置。基于此,您的设置看起来不错。纯猜测:您的 DocumentRootScriptAliasmatch 指向同一个目录,这可能会使 Apache 感到困惑。尝试使用不同的DocumentRoot
  • 我已经解决了这个问题。我错过了 $ 正则表达式终止符,但我还需要直接指向 index.html 而不仅仅是 website 文件夹。感谢大家的回复!

标签: regex apache url redirect server


【解决方案1】:
  AliasMatch "^\/\d{4}\/[a-zA-Z]+\/\d{1,2}[\/]?$" "/home/bitnami/myproject/dist/mywebsite/index.html"

它反复破坏和重复 index.html 的原因是因为我错过了 $ 终止符。还添加了 Bren 的一些建议。

【讨论】:

    猜你喜欢
    • 2011-10-17
    • 1970-01-01
    • 2019-06-23
    • 2022-11-01
    • 2011-10-07
    • 1970-01-01
    • 1970-01-01
    • 2011-04-13
    • 1970-01-01
    相关资源
    最近更新 更多