1

The homepage/base url of the app can be refreshed with no issues. but other pages return 404 on page refresh. Please are there any work around for this?

Here is a screenshot of the 404. [1]: https://i.sstatic.net/lc8xD.png

2
  • 1
    Can you please show the <template> and <script> of the component/page where the 404 occurs? Commented Apr 24, 2022 at 15:31
  • It happens to all pages thats not the base url... I dont think it has anything to do with code, I think it has to do with the virtual url vue-router history mode sets in the browser url bar. They're not real so when the page refreshes, http server tries to load the url and fails.. There must be an all-server acceptable work around for this. Commented Apr 24, 2022 at 15:38

6 Answers 6

5

It happens because Vue routing is only on the frontend side. So when you refresh the page it goes backend server. And it checks for any files to satisfy the request.

To solve this issue. You have to tell your apache webserver to handle it for your front end Vue app.

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

4 Comments

How do i do that please...?
Check this link sanakil.blogspot.com/2021/03/… . It will help you. I have written it for the subfolder but you can use it with the base URL(/) too. You just have to set up an apache configuration.
Thanks. It doesnt bring an error message now. It just shows blank.
@Jacky001 have you resolve the issue? I'm hosting my web now and getting the same issue.
3

The solution that worked for me:

In /etc/apache2/sites-available/"yourAppDomain".conf add to the end:

FallbackResource /index.html
</VirtualHost>

Comments

1

Try adding this to your .htaccess:

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

2 Comments

It doesnt work even after refreshing apache server. However error text is not displaying. It just blank and doesnt redirect.
I believe this can guide you to the solution: router.vuejs.org/guide/essentials/…
0

Page refresh returns 404 error VueJs (mode:history) with Laravel:

The solution that worked for Laravel:

Assuming your entry point is the index method, you just need to add this route at the bottom of your routes/web.php file.

Route::get('{any}', function () {
    return view('welcome');
})->where('any','.*');

Comments

0

I found solution which works for me and router works too :)

IIS setup rewrite rule:

<rule name="MY SPA APP REWRITE RULE" stopProcessing="true">
    <match url="myAppBaseURL/(.*)" />
    <conditions logicalGrouping="MatchAll">
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    </conditions>
    <action type="Rewrite" url="/myAppBaseURL?rewriteURL={R:1}" />
</rule>

And then in the main App.vue add this

import { useRouter } from 'vue-router'
const router = useRouter()

if(new URLSearchParams(window.location.search).get('rewriteURL')){
  router.push(new URLSearchParams(window.location.search).get('rewriteURL'))
}

Comments

0

with nginx set your server for vue router history mode https://router.vuejs.org/guide/essentials/history-mode.html#nginx

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

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.