Uri_helper
This utility library offers a set of functions for managing URLs, handling redirects, sanitizing input, generating slugs, and processing request URIs in web applications.
Nginx in Uri_helper
Uri_helperSince Uri_helper provides functions for handling request URIs and generating structured URLs, proper Nginx configuration is required to remove index.php from URLs and allow request_uri() to function correctly.
Pretty URLs
location / {
try_files $uri $uri/ /index.php?$query_string;
}If the requested URL matches a file or directory, it serves it directly.
Otherwise, the request is rewritten to
index.php, ensuringrequest_uri()processes a clean and structured URL.
This ensures URLs like:
https://phpfast.net/products?page=2 remain structured without unnecessary slashes or index.php in the URL.
Handling Redirects Efficiently
The redirect() function in Uri_helper uses PHP’s header('Location: ...') method for redirection. However, on Nginx, it is possible to leverage server-level redirection for efficiency.
Optimizing Redirects in Nginx Instead of handling all redirects in PHP, you can configure Nginx to process common redirects like forcing HTTPS or redirecting old URLs:
server {
listen 80;
server_name phpfast.net;
return 301 https://$server_name$request_uri;
}Redirects HTTP to HTTPS without relying on PHP.
Reduces the need for
redirect()calls in the application, improving performance.
Supporting Clean Request URIs
The request_uri() function in Uri_helper ensures clean URL formatting, removing unnecessary slashes and normalizing paths. However, Nginx needs to pass the correct $_SERVER['REQUEST_URI'] to PHP.
Ensure Correct Request URI Handling in Nginx
fastcgi_param REQUEST_URI $request_uri;Ensures
request_uri()receives the correct request path.Prevents duplicate slashes (
//) in URLs from being processed incorrectly.
Example Usage with request_uri() in PHP:
echo request_uri('https://phpfast.net//user/info');
// Output: user/infoWithout proper configuration, Nginx may pass incorrect paths, leading to unexpected results in request_uri().
Functions in Uri_helper
request_uri()
request_uri()Retrieves the clean request URI from
$_SERVER['REQUEST_URI'].Removes extra slashes and normalizes the path.
Redirects if necessary to enforce a clean URL structure.
Example:
echo request_uri('https://phpfast.net/user/info/');
// Output: user/info/
echo request_uri('https://phpfast.net/user//info//');
// Output: user/info/
echo request_uri('https://phpfast.net/user/info?page=3');
// Output: user/info/
echo request_uri('https://phpfast.net/');
// Output: emptybase_url()
base_url() Generates the base URL for the application.
Retrieves the
app_urlfrom the config file if not already stored in$base_url.Trims extra slashes and handles query strings, ensuring a clean and structured URL.
echo base_url('products?page=2');
// Output: https://phpfast.net/products?page=2redirect()
redirect()Redirects the user to another URL using the
header()function.exit();ensures that no further code is executed after redirection.
Example:
redirect('https://phpfast.net/dashboard');sanitize_url()
sanitize_url()Sanitizes URLs by removing invalid characters.
Uses
FILTER_SANITIZE_URL, a built-in PHP filter.
Example:
echo sanitize_url('https://phpfast.net/<script>alert(1)</script>');
// Output: https://phpfast.net/ AND alert(1)url_slug()
url_slug()Converts UTF-8 strings into SEO-friendly slugs.
Removes special characters, spaces, and accents (e.g.,
Đ→D).Replaces spaces with a delimiter (
-by default).
Example:
echo url_slug('New Products in 2025!');
// Output: new-products-in-2025Function
Purpose
base_url()
Generates the base URL of the application.
redirect()
Redirects to another URL.
sanitize_url()
Cleans a URL from invalid characters.
url_slug()
Converts a UTF-8 string into a slug.
request_uri()
Retrieves the clean request URI.
Last updated
Was this helpful?