【问题标题】:.htaccess rewrite to index.php or index.html based on condition.htaccess 根据条件重写为 index.php 或 index.html
【发布时间】:2022-02-04 23:28:57
【问题描述】:

我对@9​​87654321@ 不太满意,并试图找到我的问题的答案,但到目前为止还没有运气。

所以我有这个.htaccess重写:

RewriteCond %{REQUEST_URI} /(api|nova|nova-api)
RewriteRule .* /index.php

效果很好。

该网站是一个 Angular 网站,其中我有由 JS 路由的动态 URL。

因此,如果我打开基域:example.com 效果很好,因为 index.html 已提供服务。

但是,如果我打开如下路线:example.com/example-route。它说 404。

您能帮我修改.htaccess文件吗?

【问题讨论】:

  • 我不明白,你的 Laravel Api 和 Angular 项目在同一个文件夹中?
  • 不,它是分开的。这是我从开发团队接手的遗留项目。他们使用 nginx 进行重写,但我不得不迁移到 apache。

标签: laravel apache .htaccess mod-rewrite


【解决方案1】:
RewriteCond %{REQUEST_URI} /(api|nova|nova-api)
RewriteRule .* /index.php

您似乎只需要将请求重写为 index.html您的 API 重写为 index.php。但是,您应该修改现有规则以添加 L 标志,并且应该锚定与请求匹配的正则表达式(尽管根本不需要 条件,因为应该在RewriteRule 指令本身)。

例如,请尝试以下操作:

# "index.html" needs to take priority when requesting the root directory
DirectoryIndex index.html

# Abort early if request is already "index.html" or "index.php"
RewriteRule ^index\.(html|php)$ - [L]

# Rewrite certain requests to Laravel API
RewriteRule ^(api|nova|nova-api)($|/) index.php [L]

# Rewrite everything else to Angular
# (unless it already maps to a file or directory)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.html [L]

由于“主页”已经可以正常工作,因此建议 DirectoryIndex 在服务器配置中已设置正常(并优先考虑 index.html),尽管明确将此设置为 index.html(如上)更多最佳,如果这就是所有需要的话。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多