0

I'm trying to create a site that is loading all it's content via Ajax. So let's say the site is www.abc.net.

I have abc.net/index.html and this file shall always being called, whatever URL(folder/file) was typed in. So e.g. abc.net/somesubsite was called, the URL should stay as it is and just calling the index.html.

The idea is now to look which URL was called and to handle that with jQuery and showing the specific div. In that way a direct link would be easily possible.

Well my issue is that I'm not able to let the URL stay as it is and when having abc.net/somesubsite/someother then I get an server error. I was playing around with htaccess but it feels that it's not really a perfect solution.

Is there good way to do that? It is like this site but I want to be able to handle folder/subfolder which is not possible there. I'm interested in any solution, it's also no problem to have different html files that are loaded if really needed. But if possible I would like to have all in one file.

Yotam Omer
  • 15,310
  • 11
  • 62
  • 65
kwoxer
  • 3,734
  • 4
  • 40
  • 70
  • Perhaps you'd like to look into a framework for developing single-page web applications, such as [AngularJS](http://angularjs.org)? – A. Duff Feb 18 '15 at 21:07
  • Well might be an idea but no solution because there I would have the same issues with deep linking I think. Also figured out now how to do that with simple javascript =) – kwoxer Feb 19 '15 at 11:09

1 Answers1

1

Update your .htaccess file like so:

RewriteEngine On 
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]

This will redirect all relevant server calls to index.php?url=<rest of the url>.

Now, rename your index.html to index.php (we need to write a little php code).

In your JavaScript write:

var url = "<?php echo $_GET['url']; ?>";

And there you have it. You can now split the string by '/' delimiter to find all your needed components.

As per your example, the following url: abc.net/somesubsite/someother will redirect to index.php and the url variable will hold somesubsite/someother.

You can then use url.split('/') to get all "subfolders" in an array.

You will also have to override all links' behavior in your site:

  1. Prevent the normal action
  2. Load the ajax content
  3. pushState to change the url

like so:

$('a').on('click', function(e){ 
    e.preventDefault(); // 1
    var $url = this.href;
    loadContent($url); // 2 
    window.history.pushState("object or string", "Title", $url); // 3        
});
Yotam Omer
  • 15,310
  • 11
  • 62
  • 65
  • When I go abc.net/somesubsite then the index.php is redirected. When I go abc.net/somesubsite/somesite it does not load the css stuff correctly. How to fix that? I mean both is bad for me. I need a hidden redirect and yeah fixed css pathes =/ – kwoxer Feb 18 '15 at 21:03
  • 1
    use html `` tag in your `` section to set the base address of your site. The htaccess redirect I gave you will not redirect .css files. The first line that ends with `! -f` means: If this url doesn't point to a file – Yotam Omer Feb 18 '15 at 21:06
  • Cool, thanks I didn't know. Okay so that fixed the multiple subfolder issue. But still when I go abc.net/somesubsite I'm redirect to the index.php. How can I fix that? – kwoxer Feb 18 '15 at 21:08
  • 1
    Do you have it live so I can see the problem? – Yotam Omer Feb 18 '15 at 21:09
  • Not really, sorry. And there is another issue. I have for sure links like abc.net/somesubsite/somesite but now this is not Ajax on page loaded anymore. It's now completely reloading the page, maybe that's the reason? – kwoxer Feb 18 '15 at 21:15
  • 1
    To do that you will have to override the links' click event.. look at [this question](http://stackoverflow.com/questions/824349/modify-the-url-without-reloading-the-page) for changing the url without redirecting the page. – Yotam Omer Feb 18 '15 at 21:18
  • 1
    Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/71209/discussion-between-yotam-omer-and-kwoxer). – Yotam Omer Feb 18 '15 at 21:19