Home > Enterprise >  Vue.js Apache , how to write redirect rules for multiple entries?
Vue.js Apache , how to write redirect rules for multiple entries?

Time:10-10

I have a vue.js app which consists of 2 entry points (2 SPA's). one of them is index.html, which serves the client website and the other is for the platform (platform.html)

Following the Vue CLI documentation, I am using the following in my .htacess file

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

this only works if I only have index.html and when I type in myUrl.com/platform it doesn't change to the platform. I have tried the following and can't make it work

<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>

CodePudding user response:

Assuming that any URL prefixed with platform should be sent to platform.html and everything else to index.html then you could do something like the following in your root .htaccess file:

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]

The RewriteBase directive is not required here.

You do not need the <IfModule> wrappers, unless these directives are optional (they are not). (It doesn't make sense to nest these wrappers in this way.) See my answer to the following question on the Webmasters stack for more discussion on this: Is Checking For mod_write Really Necessary?

Note that requests for the document root are not actually rewritten by the directives above, but are instead sent to index.html by the DirectoryIndex directive - this is often already configured on the server, so the directive may not be required here.

UPDATE: Since your URLs are like /platform/<subroute>, rather than /platform<something>, the regex in the above rule would be better as ^platform($|/), rather than simply ^platform, in order to match the whole path segment only and to avoid potentially matching too much, such as /platformsomething. (I've updated the code above.)

^platform($|/) matches platform or platform/something, but not platformsomething.

  • Related