Question

Sorry for the badly worded question.

PHP with apache uses index.php/index.html for directory urls like: localhost = localhost/index.php or localhost/place = localhost/place/index.php

If I start with: localhost/place and I use a javascript history.pushstate to update a url with a long adress like: localhost/place/subplace then if I enter that url in the browser I'll go to localhost/place/subplace/index.php when I really wanted localhost/place/index.php to allow that url to be the only point of entry.

I'm using simple javascript(window.location.pathname or anchorNode.pathname) to retrieve the url path for use with ajax. This is used by a simple router similar to backbone.js to update the page. The javascript routing works and back/forward in the browser works. If only I could get it to work with the single point of entry for urls entered in the address bar.

To sum up:

I want a single point of entry for my php app to get all subdirectories.

At the single point of entry I want to run the javascript to acquire the path and use it to route the page with ajax.

I'm using history.pushState to update the url, but that messes with the single point of entry for the app when the directories are longer than the main directory. Basically I get a 404 page.

Right now I'm not too concerned with making it backwards compatible with browsers that don't have history.pushState. I just want this one thing to work.

As an addendum I would prefer working with regular paths in javascript and not the query string. Whether the page is loaded with the address bar or the history.pushState is used that's what I would prefer. I don't know if this can be handled with apache rewrite or what.

Similar questions:

How to cope with refreshing page with JS History API pushState

Was it helpful?

Solution

Ok. I'm making things too hard.

To get the routing to work on page load I need to do two things.

In the .htaccess file I can use apache rewrite to make all urls route to index.php?path=first/second/third.

When the page loads just concatenate the the new path in the query string to the javascript string that handles the route.

The javascript is still being used, and there's no duplication of functionality. Everything is good.

This also kind of answers this: How to cope with refreshing page with JS History API pushState

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top