ionic-framework: Linking to a deep link fails when used with "path" URL strategy

Ionic version: (check one with “x”) [ ] 1.x [X] 2.x

I’m submitting a … (check one with “x”) [X] bug report [ ] feature request [ ] support request => Please do not submit support requests here, use one of these channels: https://forum.ionicframework.com/ or http://ionicworldwide.herokuapp.com/

Current behavior: When in development with ionic serve and using the default hash URL strategy, I can configure a segment like this segment: 'product/:productId'. I can successfully navigate to this page and the URL looks like this…http://localhost:8100/#/product/12876. When I paste the URL into a new browser tab, it comes up again. BTW, beautiful job. I’m so stoked about the simplicity of this “router”.

When I change the hash strategy to { locationStrategy: 'path' } and set <base href = '/'> in my index.html file, everything works great when navigating in the app. However, I can no longer reach the page when I paste the http://localhost:8100/product/12876 URL into another browser tab. I’m getting a 404.

I have not tested this outside my localhost environment.

Expected behavior: Deep linking should work when using the path locationStrategy.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 8
  • Comments: 34 (4 by maintainers)

Commits related to this issue

Most upvoted comments

When will this release be live ? This is a major issue

I Think that configuration of the redirect can be done in @app-scripts/dist/dev-server/http-server.js when configuring the express server. Edit: I managed to do it but adding app.all('/*', serveIndex); a the end of this function:

function createHttpServer(config) {
    var app = express();
    app.set('serveConfig', config);
    app.listen(config.httpPort, config.host, function () {
        logger_1.Logger.debug("listening on " + config.httpPort);
    });
    app.get('/', serveIndex);
    app.use('/', express.static(config.wwwDir));
    app.use("/" + serve_config_1.LOGGER_DIR, express.static(path.join(__dirname, '..', '..', 'bin'), { maxAge: 31536000 }));
    // Lab routes
    app.use(serve_config_1.IONIC_LAB_URL + '/static', express.static(path.join(__dirname, '..', '..', 'lab', 'static')));
    app.get(serve_config_1.IONIC_LAB_URL, lab_1.LabAppView);
    app.get(serve_config_1.IONIC_LAB_URL + '/api/v1/cordova', lab_1.ApiCordovaProject);
    app.get('/cordova.js', servePlatformResource, serveMockCordovaJS);
    app.get('/cordova_plugins.js', servePlatformResource);
    app.get('/plugins/*', servePlatformResource);
    if (config.useProxy) {
        setupProxies(app);
    }
    app.all('/*', serveIndex);
    return app;
}

I’m sure that, guys from app script can cookup something to check if there is a base in the head of the HTML and add this line programatically, or even simpler add a config for it. Maybe someone can check how Angular team does it when you use the router and ng serve (if they do)

I don’t think this is a bug, but it would be great, if this “issue” would be pointed out in the docs as it isn’t obvious, I guess.

I’d suggest something like:

  • If you set “locationStrategy” to “path” (removes hashbangs when using Deeplinking), you have to make sure that your webserver rewrites URL’s to “index.html”.
  • In apache this can be achieved using this “.htaccess” file configuration…
  • If you run your app locally (ionic serve), locationStrategy “path” is not working (currently).

Just to be clear, we are all now talking about Ionic 3, aren’t we? I’ve got this exact problem too (on Ionic 3). It’s a critical bug due to our Oauth provider not handling hash based URLs - because it uses the hash for its own purposes.

This issue will not be resolved in v3.

The next major release of Ionic and Ionic for Angular will change navigation such that we use the Angular router and the Angular CLI which should fix this issue.

If this is still an issue at that time, please log a new issue. I am going to close this one as something that will not be fixed in the current major release.

Does your webserver rewrite “subdirectories” (like “/product”) to index.html?

Make sure it does, the URLs have to be rewritten… I guess this is a precondition to get PathLocationStrategy-routing without the Hash (example.com/#/product) working… Otherwise the webserver is looking for a subdirectory “product” (therefore 404) and the ionic logic won’t ever be called in that case… Or am I missing something?

e.g. .htaccess Apache:

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

(I got it working that way, see also https://forum.ionicframework.com/t/how-to-use-deeplinker-in-browser/75787/7) Please correct me if I am wrong.

Arno

@erperejildo - the v4 alpha is getting close. After that it is all a question of how well things go with that.

@kensodemann I think that it’s a major issue because of SEO. I’m very concerned that a hashbangs (using hashlocation strategy) will broke any attempt to be scrapped by robots. Using a alpha release is not a option.

I am building a PWA app along with Android and iOs build and I integrated Google login but google Oauth credentials doesn’t support redirect URI along with #. Currently, we are using ionic serve but not sure how will it be in web and mobile app. Please do let me know if someone fixed it.

@kensodemann Would it be possible to leave this issue open until the fix is released? This would help me (and other users) have better visibility as to when this issue truly resolved. Thanks for your help in advance!

Is there a way that you can simply enable URL-based routing for local dev? I found this https://github.com/driftyco/ionic-plugin-deeplinks but installing and configuring a plugin seems like overkill just to prevent live reload from redirecting back to the root page every edit.

@johnhny Thanks for sharing, that resolved it for me. Just to note for others, this does not resolve the issue when running in Ionic serve, but when serving the files via apache, i.e. when hosting on a server, or locally.