Vue.js + Apache ,如何为多个条目编写重定向规则?
Vue.js + Apache , how to write redirect rules for multiple entries?
我有一个 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
的 URL 应该被发送到 platform.html
而其他所有内容都被发送到 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
指令。
您不需要 <IfModule>
包装器,除非这些指令是 可选的(它们不是)。 (以这种方式嵌套这些包装器没有意义。)参见 my answer to the following question on the Webmasters stack for more discussion on this: Is Checking For mod_write Really Necessary?
请注意,对文档根目录的请求实际上并没有被上面的指令重写,而是被 DirectoryIndex
指令发送到 index.html
- 这通常已经在服务器上配置了,所以此处可能不需要该指令。
更新: 由于您的 URL 类似于 /platform/<subroute>
,而不是 /platform<something>
,上述规则中的正则表达式会更好如^platform($|/)
,而不是简单的^platform
,以便仅匹配整个路径段并避免可能匹配太多,例如/platformsomething
。 (我已经更新了上面的代码。)
^platform($|/)
匹配 platform
或 platform/something
,但不匹配 platformsomething
.
我有一个 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
的 URL 应该被发送到 platform.html
而其他所有内容都被发送到 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
指令。
您不需要 <IfModule>
包装器,除非这些指令是 可选的(它们不是)。 (以这种方式嵌套这些包装器没有意义。)参见 my answer to the following question on the Webmasters stack for more discussion on this: Is Checking For mod_write Really Necessary?
请注意,对文档根目录的请求实际上并没有被上面的指令重写,而是被 DirectoryIndex
指令发送到 index.html
- 这通常已经在服务器上配置了,所以此处可能不需要该指令。
更新: 由于您的 URL 类似于 /platform/<subroute>
,而不是 /platform<something>
,上述规则中的正则表达式会更好如^platform($|/)
,而不是简单的^platform
,以便仅匹配整个路径段并避免可能匹配太多,例如/platformsomething
。 (我已经更新了上面的代码。)
^platform($|/)
匹配 platform
或 platform/something
,但不匹配 platformsomething
.