John Davidson

.htaccess - URL rewriting with PHP

0 comments
Message:


I have a URL that looks like:



url.com/picture.php?id=51


How would I go about converting that URL to:



picture.php/Some-text-goes-here/51


I think WordPress does the same.



How do I go about making friendly URLs in PHP?



My Answer:
To achieve friendly URLs in PHP, you can use URL rewriting in your .htaccess file. Here's an example of how you can rewrite the URL from "picture.php?id=51" to "picture.php/Some-text-goes-here/51":

1. Create or edit your .htaccess file in the root directory of your website.

2. Add the following code to your .htaccess file:


RewriteEngine On
RewriteRule ^picture.php/([^/]+)/([0-9]+)$ picture.php?id=$2 [L]


3. Save the .htaccess file and test the new URL structure by accessing "url.com/picture.php/Some-text-goes-here/51" in your browser.

This code snippet uses regular expressions to match the desired URL structure and rewrite it to the original format with the query parameter. You can customize the regular expressions to fit your specific needs and URL structure.

WordPress uses a similar approach to create friendly URLs for its posts and pages. By using URL rewriting, you can make your URLs more user-friendly and improve SEO by including relevant keywords in the URL.

Rate this post

5 of 5 based on 3392 votes

Comments




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