Search code examples
phpmod-rewritefriendly-url

Pretty URL specific example


I've been searching for specific examples to answer my question about pretty URLs. Unfortunately, no luck. If anyone can help point me in the right direction that would be great.

Currently my URL is: https://mywebsite.com/offers/details/?id=1234

Ideally, I would like it to just include the title of the offer: https://mywebsite.com/offers/details/truffle-hunter

BUT - I'm pretty sure that I need to include the id so that the query can be made for the details of the offer (can't query by the name due to possible duplicates) so I think: https://mywebsite.com/offers/details/1234/truffle-hunter

Question is how would this actually work? Do I do a mod_rewrite so that any URL that includes '/offers/details/' redirects to the index page inside the folder 'details'? How do I do that and pass the id '1234' along? Assuming that from there I would use $_GET('id') to grab the value?

This is my first experience with mod_rewrite, have a little experience with .htaccess, and a super basic understanding of regex - so please be gentle. Thanks for any help.


Solution

  • You have to use rewrite in .htaccess file. Create a .htaccess file with the code below The .htaccess file needs to be placed in mywebsite.com's root directory

    example 1:

    https://mywebsite.com/offers/details-id-1234.htm

    Options +FollowSymLinks
    RewriteEngine on
    RewriteRule ^offers/details-id-(.*).htm$ /offers/details.php?id=$1
    

    example 2:

    https://mywebsite.com/offers/details/id/1234/

    Options +FollowSymLinks
    RewriteEngine on
    RewriteRule ^offers/details/id/(.*) /offers/details.php?id=$1
    RewriteRule ^offers/details/id/(.*)/ /offers/details.php?id=$1