How can i change url without reloading page in php?

I have http://mysite.com/index.php.

And a sub menu

  • home => http://mysite.com/index.php
  • about us => http://mysite.com/about.us.php
  • products => http://mysite.com/products.php

But i want http://mysite.com/index.php to process every request, and just change the content using Ajax request. This way, the site only loads the content part, and is much faster and easy to navigate.

The problem here is SEO, because the only URL google will see is http://mysite.com/index.php and I would like to associate http://mysite.com/about-us to the About Us content, http://mysite.com/product to the Products content, etc.

I know I can do this with PHP just reading the URL and writing the Ajax on the fly, but doing so the whole page is going to be reloaded every time. Is there a way to do this without reloading the whole page? What I think I need is to have a regular anchor in the submenu, for exampel pointing to "http://mysite.com/contact-us" but when clicked, instead of opening this page, process the Ajax request.

And if this is possible, Google is going to see this as black hat probably, right?

Regards Alex

asked Dec 17, 2010 at 22:38

How can i change url without reloading page in php?

Alex AngelicoAlex Angelico

3,4168 gold badges30 silver badges47 bronze badges

3

This is a routing issue, not an AJAX issue.

If you were using another tool (cough ASP.NET MVC cough), you'd just add a route (and I'm hopeful there's a way to do this in PHP) that accepted URLS like

/home
/products
...

and routed them to, say,

/index.php?area=home
/index.php?area=products

This is typically accomplished with a rewrite engine when used outside of a good MVC or RESTful URL system. I use ISAPI Rewrite on IIS, but if you're working on the LAMP stack, I think Apache provides a module that provides the same capabilities. (Google .htaccess )

WARNING: RANT FOLLOWS

And, for what it's worth,

  1. Avoid trying to write your entire application in JavaScript. The server's there for a reason. Part of your job as a web developer is to absorb as much of the work onto your server as possible. Browser performance and compatibility issues will drive you mad when you try to do everything on the client.

  2. Avoiding postbacks makes sense in a lot of circumstances, but it's not a silver bullet that you should try to apply to every page. Usually it makes sense to load a new page when a link is clicked. It's what the user expects, it's more stable (since most of the infrastructure required is server-side) and it's not slower than an AJAX request to retrieve the same thing.

Rules:

  1. NEVER break the back button. Without careful planning, most AJAX apps break this rule.
  2. See rule #1.

answered Dec 17, 2010 at 22:44

How can i change url without reloading page in php?

3Dave3Dave

27.9k18 gold badges84 silver badges150 bronze badges

1

How does Shopify do it then? Go to their website, click on the Features link and you'll see the URL says:

http://www.shopify.com/tour/sell-online

Then click on any of the sub links and you'll see that the address in the URl changes without using a hash but there is no page flip.

I don't think they are using ajax to change the content because it all appears to be included in hidden divs on the page, but regardless, you can apparently change the URL using client side tricks.

answered Nov 16, 2012 at 18:07

Julian DormonJulian Dormon

1,6374 gold badges31 silver badges55 bronze badges

I would like to access the portion before the # hash if possible.

I only need to change the portion after the domain, so it's not like I'm violating cross-domain policies.

 window.location.href = "www.mysite.com/page2.php";  // Sadly this reloads

Is there a way I can modify the URL of the current page without reloading the page?

How can i change url without reloading page in php?
Jun 20, 2020 in Java-Script by
• 37,510 points
3,817 views

1 answer to this question.

Hii @kartik,

HTML5 introduced the history.pushState() and history.replaceState() methods, which allow you to add and modify history entries, respectively.

window.history.pushState('page2', 'Title', '/page2.php');

Hope this is helpful!!

How can i change url without reloading page in php?
answered Jun 20, 2020 by Niroj
• 82,780 points

  • All categories
  • How can i change url without reloading page in php?
    Apache Kafka (84)
  • How can i change url without reloading page in php?
    Apache Spark (596)
  • How can i change url without reloading page in php?
    Azure (131)
  • How can i change url without reloading page in php?
    Big Data Hadoop (1,907)
  • How can i change url without reloading page in php?
    Blockchain (1,673)
  • How can i change url without reloading page in php?
    C# (124)
  • How can i change url without reloading page in php?
    C++ (268)
  • How can i change url without reloading page in php?
    Career Counselling (1,060)
  • How can i change url without reloading page in php?
    Cloud Computing (3,356)
  • How can i change url without reloading page in php?
    Cyber Security & Ethical Hacking (145)
  • How can i change url without reloading page in php?
    Data Analytics (1,266)
  • How can i change url without reloading page in php?
    Database (853)
  • How can i change url without reloading page in php?
    Data Science (75)
  • How can i change url without reloading page in php?
    DevOps & Agile (3,500)
  • How can i change url without reloading page in php?
    Digital Marketing (111)
  • How can i change url without reloading page in php?
    Events & Trending Topics (28)
  • How can i change url without reloading page in php?
    IoT (Internet of Things) (387)
  • How can i change url without reloading page in php?
    Java (1,148)
  • How can i change url without reloading page in php?
    Kotlin (3)
  • How can i change url without reloading page in php?
    Linux Administration (384)
  • How can i change url without reloading page in php?
    Machine Learning (337)
  • How can i change url without reloading page in php?
    MicroStrategy (6)
  • How can i change url without reloading page in php?
    PMP (423)
  • How can i change url without reloading page in php?
    Power BI (516)
  • How can i change url without reloading page in php?
    Python (3,154)
  • How can i change url without reloading page in php?
    RPA (650)
  • How can i change url without reloading page in php?
    SalesForce (92)
  • How can i change url without reloading page in php?
    Selenium (1,569)
  • How can i change url without reloading page in php?
    Software Testing (56)
  • How can i change url without reloading page in php?
    Tableau (608)
  • How can i change url without reloading page in php?
    Talend (73)
  • How can i change url without reloading page in php?
    TypeSript (124)
  • How can i change url without reloading page in php?
    Web Development (2,989)
  • How can i change url without reloading page in php?
    Ask us Anything! (66)
  • How can i change url without reloading page in php?
    Others (994)
  • How can i change url without reloading page in php?
    Mobile Development (37)

Subscribe to our Newsletter, and get personalized recommendations.

Already have an account? Sign in.

How do I modify the URL without reloading the page?

Method 2: Adding a new state with pushState() Method: The pushState() method is used to add a new history entry with the properties passed as parameters. This will change the current URL to the new state given without reloading the page.

How can I change the current URL in PHP?

php if (! $_GET["name"] && $_GET["id"]) { // perform MySQL query to get name based on id header("Location: http://www.something.org/zzz"); die(); // stop execution of this page } // if we got here, $_GET["name"] is set, so do whatever this script is supposed to do. ?> NOTE: The above .

How do I change the URL after Ajax success?

You can do this to your success action : window. history. pushState("object or string", "Title", "/new-url");

How do I change the URL on my browser?

There are basically two ways to change the URL displayed in the browser* through javascript without reloading the page..
window.history.pushState(“object or string”, “Title”, “/new-url”);.
window.history.replaceState(“object or string”, “Title”, “/another-new-url”);.