John Davidson

php - Remove query string parameter to create a more user-friendly URL

0 comments
Message:


I have this .htaccess file and I have no knowledge with mod_rewrite,


RewriteEngine On
RewriteRule ^(.+[^/])/$ http://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{THE_REQUEST} \s/([^.]+)\.php [NC]
RewriteRule ^ /%1 [NE,L,R]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)/?$ /$1.php [L]

What I want to achieve is to have localhost/viewticket/${id} instead of localhost/viewticket.php?id=123


I have tried many .htaccess rules but none worked except this one that hides .php in my URL.



My Answer:
To remove the query string parameter and create a more user-friendly URL, you can modify your .htaccess file as follows:

apache
RewriteEngine On

# Remove query string parameter
RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule ^viewticket.php$ /viewticket/%1? [R=301,L]

# Rewrite user-friendly URL to actual PHP file
RewriteRule ^viewticket/([0-9]+)$ viewticket.php?id=$1 [L]


With this configuration, when a user accesses localhost/viewticket.php?id=123, they will be redirected to localhost/viewticket/123 and the server will internally serve the content from viewticket.php with the id parameter. This will create a more user-friendly URL without the query string parameter.

Rate this post

3 of 5 based on 6716 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us