0

Currently working on a Vue 3 app which deployed using Azure web apps. When I deployed it to the root and used web.config it worked fine, But I need to add a Base URL. For that, I have added the following for the vite.config:

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue

export default defineConfig({
  plugins:[vue()],
  base: 'sub-url'
})

But this failed to rewrite.

web.config:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Handle History Mode and custom 404/500" stopProcessing="true">
          <match url="(.*)" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
          </conditions>
          <action type="Rewrite" url="/sub-url/" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

Documentation

Currently I'm using Hash Mode as a workaround but I need to use HTML version. The app works in Hash mode and locally without any errors.

1 Answer 1

0

Here you need to Configure the web.config to handle the HTML5 history mode and rewrite URLs correctly, Especially when using a subdirectory.

  • Any request starting with /sub-url/ is rewritten to /sub-url/index.html.

Check the below web.config for a Vue 3 app deployed in a subdirectory using Vite.

web.config:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Handle History Mode and custom 404/500" stopProcessing="true">
          <match url="^(sub-url)/(.*)$" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
          </conditions>
          <action type="Rewrite" url="/sub-url/index.html" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

Vue Router is configured to use the same base URL.

import { createRouter, createWebHistory } from 'vue-router'
import routes from './routes'

const router = createRouter({
  history: createWebHistory('/sub-url/'), // Use the same base URL
  routes,
})

export default router

Configure the base URL with a leading and trailing slash.

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

export default defineConfig({
  plugins: [vue()],
  base: '/sub-url/', // Ensure leading and trailing slash
})

Result:

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

I did the same thing yet it does not work. It throws a 500 error, 'The page cannot be displayed because an internal server error has occurred'
please refresh or restart your app once it re-deployed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.