php remove parameter from url code example
Example 1: remove .php from url
# Apache Rewrite Rules
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# Add trailing slash to url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)$ $1/ [R=301,L]
# Remove .php-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)/$ $1.php
# End of Apache Rewrite Rules
</IfModule>
Example 2: remove get parameters from url php
function strip_param_from_url( $url, $param ) {
$base_url = strtok($url, '?'); // Get the base url
$parsed_url = parse_url($url); // Parse it
$query = $parsed_url['query']; // Get the query string
parse_str( $query, $parameters ); // Convert Parameters into array
unset( $parameters[$param] ); // Delete the one you want
$new_query = http_build_query($parameters); // Rebuilt query string
return $base_url.'?'.$new_query; // Finally url is ready
}
// Usage
echo strip_param_from_url( 'http://url.com/search/?location=london&page_number=1',
'location' )