【问题标题】:Vue.js + Apache , how to write redirect rules for multiple entries?Vue.js + Apache,如何为多个条目编写重定向规则?
【发布时间】:2021-11-24 08:02:30
【问题描述】:

我有一个 vue.js 应用程序,它包含 2 个入口点 (2 SPA's)。其中一个是index.html,为客户网站服务,另一个为平台(platform.html)

按照 Vue CLI 文档,我在 .htacess 文件中使用以下内容

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.html$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /index.html [L]
</IfModule>

这仅在我只有 index.html 并且当我输入 myUrl.com/platform 时才有效,它不会更改为平台。 我尝试了以下方法,但无法正常工作

<IfModule mod_rewrite.c>
  <IfModule mod_rewrite.c>
  RewriteEngine On
  
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^platform(.*)$ platform.html [L]
  RewriteRule ^(.*)$ index.html [L]

</IfModule>
</IfModule>

【问题讨论】:

  • 两个平台的网址是什么?是/platform/&lt;something&gt; 还是/platform&lt;something&gt;(您的问题可能暗示)?
  • @MrWhite 是这样的/platform/&lt;subroute&gt;
  • 在这种情况下,RewriteRule pattern 会比^platform($|/) 更好,而不是简单的^platform,以避免潜在的匹配过多。我已经更新了我的答案。

标签: apache vue.js .htaccess mod-rewrite url-rewriting


【解决方案1】:

假设任何以 platform 为前缀的 URL 都应发送到 platform.html,而其他所有 URL 都应发送到 index.html,那么您可以在根 .htaccess 文件中执行以下操作:

DirectoryIndex index.html

RewriteEngine On

# Optimisation - prevent rewritten requests for the front-controller being reprocessed
RewriteRule ^index\.html$ - [L]
RewriteRule ^platform\.html$ - [L]

# Prevent static resources being routed through the app
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

# Any URLs that start "/platform" (whole path segment only) are sent to "/platform.html"
RewriteRule ^platform($|/) platform.html [L]

# All other URLs are sent to "/index.html"
RewriteRule . index.html [L]

这里不需要RewriteBase 指令。

您不需要&lt;IfModule&gt; 包装器,除非这些指令是可选的(它们不是)。 (以这种方式嵌套这些包装器没有意义。)请参阅my answer 网站管理员堆栈上的以下问题以获取更多讨论:Is Checking For mod_write Really Necessary?

请注意,对文档根目录的请求实际上并没有被上面的指令重写,而是被DirectoryIndex 指令发送到index.html - 这通常已经在服务器上配置,因此可能不需要该指令在这里。

更新:由于您的 URL 类似于 /platform/&lt;subroute&gt;,而不是 /platform&lt;something&gt;,因此上述规则中的正则表达式最好是 ^platform($|/),而不是简单的 ^platform,以便仅匹配整个路径段并避免可能匹配太多,例如/platformsomething。 (我已经更新了上面的代码。)

^platform($|/) 匹配 platform 或 platform/something,但不匹配 platformsomething。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-13
    相关资源
    最近更新 更多