0

I have the current url as: http://example.com/abcdef

When an event occurs (clicking submit button etc.), the current url needs to be updated with a different value other than "abcdef". For example, http://example.com/xaxaxa

"xaxaxa" is stored in a variable in PHP (basically from mySQL).

I know how to "get" (using $_SERVER['SCRIPT_NAME']) the current url in php, but can anyone tell how do I "set" one. I dont want to actually redirect to another page (e.g. using Location:..). I just need the url to be updated.

Mumbo Jumbo
  • 360
  • 1
  • 2
  • 12

3 Answers3

3

Cannot be done using PHP, which runs at the server-end. Use JavaScript/HTML5 for this. See location.hash and history.pushState

Anirudh Ramanathan
  • 46,179
  • 22
  • 132
  • 191
  • Cant you use .htaccess RewriteEngine in some way/form? – DMor Jul 29 '12 at 06:25
  • But that would still require that the URL change somehow, to trigger the rewrite rule. That is possible only with a server-postback and the OP isn't ready to do that with `location: ... ` – Anirudh Ramanathan Jul 29 '12 at 06:27
  • Would you be able to show me an example in Javascript..if not, a link to a similar resource would be great as well. thanks. – Mumbo Jumbo Jul 29 '12 at 06:31
  • See some of the answers [here](http://stackoverflow.com/questions/824349/modify-the-url-without-reloading-the-page). – Anirudh Ramanathan Jul 29 '12 at 06:48
  • Also, this is a brilliant tutorial to walk you through the html5 history api -> http://spoiledmilk.com/blog/html5-changing-the-browser-url-without-refreshing-page – Anirudh Ramanathan Jul 29 '12 at 06:50
1

You could try using the HTML5 history API.

https://developer.mozilla.org/en/DOM/Manipulating_the_browser_history/

0

It can be done with HTAccess to route the page properly, example code;

RewriteEngine on

#Force redirect for 404 errors to /index.php

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-l

RewriteRule ^/(.*)$ /index.php [L,QSA]

For the other issue, you'll need to update the links to include the new location in the URL.

Raiden
  • 484
  • 2
  • 8