What are fragment URLs and why to use them?

What are fragment URLs and why to use them?

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

What are fragment URLs and why to use them?

When you encounter a URL like www.example.com/foo.html#bar, the section after the hash symbol (#) is technically called a URL fragment. While it appears frequently in documentation and browser addresses, its behavior within the server-side PHP environment often leads to confusion. Understanding exactly where this data lives and how to access it is crucial for building single-page applications (SPAs), managing client-side routing, or implementing anchor links correctly.

Where does the fragment live?

The most critical distinction in web development is that the URL fragment never reaches the server.

When a browser requests www.example.com/foo.html#bar, the HTTP request sent to your PHP backend contains only:
GET /foo.html HTTP/1.1

The #bar portion is stripped away before the request leaves the client. This happens because fragments are intended solely for client-side navigation. They tell the browser which part of the document to scroll to or which script should execute, but they do not influence server logic, database queries, or file system operations.

If you attempt to read the fragment in PHP using $_SERVER['REQUEST_URI'] or $_GET, it will simply return an empty string or be completely absent. This is by design; if fragments were transmitted to the server, every link on a site would inadvertently trigger server-side logic for every anchor click, leading to unwanted database hits or infinite redirect loops.

Accessing fragments from PHP

Since PHP cannot read the fragment directly, accessing it requires client-side technology. The standard approach in modern web development is to use JavaScript. You can retrieve the current hash using window.location.hash, which returns the value including the # symbol.

const currentHash = window.location.hash; // Returns "#bar"
const cleanHash = currentHash.substring(1); // Returns "bar"

To make this accessible to your application logic, you might need to expose a simple API or use an event listener on the hashchange event. This allows you to execute specific functions when the user navigates to different sections without reloading the page.

Common Use Cases and Implementation Patterns

The primary utility of fragments is client-side routing and anchor navigation.

1. Single Page Applications (SPAs)

In a traditional multi-page site, clicking a link like contact.html#faq loads the full HTML document for that file. In an SPA using frameworks like Vue, React, or Laravel Mix/Blade with vanilla JS, fragments allow you to change the visible content without making a new HTTP request.

You can implement simple hash-based routing in PHP by rendering different views based on JavaScript state, though this is rarely done purely server-side. Instead, the server typically serves a generic index.php or a root app.html, and the JS handles switching views based on the URL path or fragment.

2. Deep Linking

Fragments enable "deep linking" to specific sections of a document. If you share a link like https://laravelcompany.com/documentation#eloquent-queries, the user lands exactly at that section. This is vital for documentation sites, FAQs, and dashboards where users expect to jump to a specific task immediately upon opening the page.

3. Email Links

Email clients often strip query parameters due to security filters, but they frequently preserve fragments. If you send an email with a link to example.com/dashboard#new-orders, the recipient's mail client might open that specific tab or section automatically. This is one of the few scenarios where fragments retain value across different user agents without server involvement.

Pitfalls and Trade-offs

While powerful, relying on fragments has limitations you must account for in your architecture.

Search Engine Limitations: Search engines like Google generally ignore URL fragments when indexing pages. If a specific section of your content is critical for SEO, relying solely on the fragment to identify that content on the server side will result in poor search visibility. The canonical content should ideally be available at a clean path without the hash if you want it indexed.

Browser Compatibility: While support for fragments is nearly universal, older mobile browsers or specific embedded views (like certain email clients) might handle the # character differently or fail to scroll to the target element entirely if the CSS for scrolling is not explicitly defined.

Security Considerations: Because fragments are visible in the browser's address bar but not on the server, they should never be used to transmit sensitive data like authentication tokens or private keys. Treating a fragment as a session identifier is a common vulnerability known as "state leakage," where an attacker could potentially manipulate the client-side view without triggering server-side validation.

Conclusion

URL fragments are a mechanism for client-side navigation and state management, not server-side logic. If you are building a PHP application with dynamic views, rely on JavaScript to handle the fragment parsing and route changes. Attempting to read $_GET or $_SERVER variables for fragment data will always yield incorrect results because the HTTP protocol intentionally separates these two layers of URL information.

Our network