Skip to content Skip to sidebar Skip to footer

Is It Possible To Do A Url Rewrite On A Dynamic Website?

I have done much research into the issue, I'm not blindly asking but I can't grasp this concept. So my website contains a single index.php file that loads data into divs via ajax s

Solution 1:

Your problem is that this would necessarily require the use of a hash, rather than a GET variable (because GET requires a page refresh, a hash doesn't). This is done via the usage of the window.location.hash variable in JavaScript, which is updated whenever the URL's content after a # changes (ex: if I were to change http://site.com/#lol to http://site.com/#lmao, window.location.hash would change from #lol to #lmao). Hashes are being used commonly in Ajax-based navigation sites, such as Twitter (I think Google's implementing it as well).

If you're using jQuery, you should try the jQuery BBQ plugin which will allow you to do things such as hash change detection (otherwise, you will have to implement some kind of similar engine yourself, because it will be needed for any kind of hash-based navigation).

You should remember, though, that this doesn't have anything to do with mod_rewrite, thus you shouldn't need to add any kind of rewrite rules. All your work (fetching data, etcetera) would be done through Ajax XML HTTP requests, rather than common HTTP requests.

Using this, you could make your url look like http://site.com/#!/post/1 (this could go whichever format you'd like, such as http://site.com/#!/p/this-is-the-posts-title) instead of http://site.com/?post=1, although you would be missing on http://site.com/post/1.


Solution 2:

Your idea of using mod_rewrite seems sound. If you used a rewrite directive which passes part of the URI as a POST variable into your index.php, couldn't you throw some code into the top of your index file which checks for that data and then dynamically generates the ajax to dump into the divs?

Ex:

RewriteRule ^(.*)$ index.php?post=$1 [L]

Index.php:

<?php if (isset($_POST['post'])) { ?>

$.ajax({ 
  **grab post content dump into div**
});

<?php } ?>

Also, don't forget to sanitize your $_POST data before processing it.


Post a Comment for "Is It Possible To Do A Url Rewrite On A Dynamic Website?"