2

I create a flutter web app and use GetX to routing.

I remove # from url with thees codes:

import 'package:url_strategy/url_strategy.dart';

void main() async {
  setPathUrlStrategy();
  runApp(const MyApp());
}

And setup pages in main.dart:

GetMaterialApp(
  initialRoute: '/home',
  getPages: [
    GetPage(
        name: '/home',
        title: "Home",
        page: () => const HomePage(),),
    GetPage(
        name: '/bet',
        title: "Bet",
        page: () => const BetPage(),),
  ],
);

And change the route with Get.toNamed("/login-signup");.

Everything is working until I serve the build folder in the Apache server. When I reload the page or enter the route directly in the browser like http://localhost/home, Error 404 happened. After searching about that, I find This Answer but it directs all routes to the home. Can you help me to fix routing in the apache server?

In my index.html file I see:

<base href="/">

And all files are in htdocs folder:

screenshot

<!DOCTYPE html>
<html>
<head>
    <base href="/">

    <meta charset="UTF-8">
    <meta content="IE=Edge" http-equiv="X-UA-Compatible">

    <!-- iOS meta tags & icons -->
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-title" content="bet">
    <link rel="apple-touch-icon" href="icons/Icon-192.png">

    <!-- Favicon -->
    <link rel="apple-touch-icon" sizes="57x57" href="./icons/apple-icon-57x57.png">
    <link rel="apple-touch-icon" sizes="60x60" href="./icons/apple-icon-60x60.png">
    <link rel="apple-touch-icon" sizes="72x72" href="./icons/apple-icon-72x72.png">
    <link rel="apple-touch-icon" sizes="76x76" href="./icons/apple-icon-76x76.png">
    <link rel="apple-touch-icon" sizes="114x114" href="./icons/apple-icon-114x114.png">
    <link rel="apple-touch-icon" sizes="120x120" href="./icons/apple-icon-120x120.png">
    <link rel="apple-touch-icon" sizes="144x144" href="./icons/apple-icon-144x144.png">
    <link rel="apple-touch-icon" sizes="152x152" href="./icons/apple-icon-152x152.png">
    <link rel="apple-touch-icon" sizes="180x180" href="./icons/apple-icon-180x180.png">
    <link rel="icon" type="image/png" sizes="192x192" href="./icons/android-icon-192x192.png">
    <link rel="icon" type="image/png" sizes="32x32" href="./icons/favicon-32x32.png">
    <link rel="icon" type="image/png" sizes="96x96" href="./icons/favicon-96x96.png">
    <link rel="icon" type="image/png" sizes="16x16" href="./icons/favicon-16x16.png">
    <link rel="manifest" href="./manifest.json">
    <meta name="msapplication-TileColor" content="#ffffff">
    <meta name="msapplication-TileImage" content="./icons/ms-icon-144x144.png">
    <meta name="theme-color" content="#ffffff">

    <title>Flutter</title>
    <link rel="manifest" href="./manifest.json">
    <link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>

<script>
    var serviceWorkerVersion = '561093598';
    var scriptLoaded = false;
    function loadMainDartJs() {
      if (scriptLoaded) {
        return;
      }
      scriptLoaded = true;
      var scriptTag = document.createElement('script');
      scriptTag.src = 'main.dart.js';
      scriptTag.type = 'application/javascript';
      document.body.append(scriptTag);
    }

    if ('serviceWorker' in navigator) {
      // Service workers are supported. Use them.
      window.addEventListener('load', function () {
        // Wait for registration to finish before dropping the <script> tag.
        // Otherwise, the browser will load the script multiple times,
        // potentially different versions.
        var serviceWorkerUrl = 'flutter_service_worker.js?v=' + serviceWorkerVersion;
        navigator.serviceWorker.register(serviceWorkerUrl)
          .then((reg) => {
            function waitForActivation(serviceWorker) {
              serviceWorker.addEventListener('statechange', () => {
                if (serviceWorker.state == 'activated') {
                  console.log('Installed new service worker.');
                  loadMainDartJs();
                }
              });
            }
            if (!reg.active && (reg.installing || reg.waiting)) {
              // No active web worker and we have installed or are installing
              // one for the first time. Simply wait for it to activate.
              waitForActivation(reg.installing || reg.waiting);
            } else if (!reg.active.scriptURL.endsWith(serviceWorkerVersion)) {
              // When the app updates the serviceWorkerVersion changes, so we
              // need to ask the service worker to update.
              console.log('New service worker available.');
              reg.update();
              waitForActivation(reg.installing);
            } else {
              // Existing service worker is still good.
              console.log('Loading app from service worker.');
              loadMainDartJs();
            }
          });

        // If service worker doesn't succeed in a reasonable amount of time,
        // fallback to plaint <script> tag.
        setTimeout(() => {
          if (!scriptLoaded) {
            console.warn(
              'Failed to load app from service worker. Falling back to plain <script> tag.',
            );
            loadMainDartJs();
          }
        }, 4000);
      });
    } else {
      // Service workers not supported. Just drop the <script> tag.
      loadMainDartJs();
    }

</script>
<script src="main.dart.js?version=73" type="application/javascript"></script>
</body>
</html>
Marlen Schreiner
  • 726
  • 10
  • 25
  • There is a official procedure to do it without the plugin, I do not know if the plugin follows it: https://docs.flutter.dev/development/ui/navigation/url-strategies There you will also find a comment at the bottom about setting `` in `index.html`: It might me necessary to use `` instead of ``. What are the Apache logs showing? Also, seeing the complete `.htaccess` file could be helpful, and knowing if the App is served from the root folder or a sub-directory. – Dabbel May 27 '22 at 11:42
  • @Dabbel I'm confused. What strategy I should have? And I want to host my flutter app in the root location. And .htaccess file is empty. – Marlen Schreiner May 27 '22 at 11:56
  • Could you post the `index.html` to check the ``? – Dabbel May 27 '22 at 11:59
  • Thanks. The idea is the Apache rewrites all paths to `index.html`. For this to work rewrite rules are defined in `.htaccess`. But for Apache actually doing these rewrites, a module called `mod_rewrite` needs to be enabled and Apache then restarted. Please check that `mod_rewrite` is enabled in your LAMPP setup. You will need to search for it where to check or enable it, I do not know enough about LAMPP. – Dabbel May 27 '22 at 12:20
  • @Dabbel I enable `mod_rewrite` but I don't know what should write in `.htaccess` – Marlen Schreiner May 27 '22 at 14:49
  • When it was not enabled before: Put the rules again in the file `.htaccess`and do a server restart (to be sure): https://stackoverflow.com/questions/70330434/flutter-web-cannot-visit-url/70358750#70358750 – Dabbel May 27 '22 at 14:50
  • @Dabbel I add that content to the `.htaccess` file, When reloading the page, the error 404 does not appear but every URL is directed to the main page. For example, I enter `localhost/bet`, and the page `localhost/home` appear. – Marlen Schreiner May 27 '22 at 15:09
  • Please try replacing the contents of `.htaccess` with these rules: https://stackoverflow.com/a/69967525/12098106 The poster claims to have solved the same problem you encounter now. – Dabbel May 27 '22 at 15:31
  • @Dabbel still I have problem with reloading and entering URLs. Every URL is directed to the main page. – Marlen Schreiner May 27 '22 at 18:09

0 Answers0