简体   繁体   中英

Custom PHP URLs from Database

i was running into a problem while creating a simple database and i hope you can provide me with some help ;).

The problem :

I need to create a database which contains offers (for example for hotels, holidays etc) thats no problem at all. What we want to do is to make each entry customizable with a single url that can be chosen by the author itself. For example we've got an overview page (overview.php) where all the offers are listed. Now i want to click on the first offer "Mallorca" for example which inherits the id 50. The dynamic URL would be something like "detail.php?id=50 for example, but i want to rewrite them from a individual field in my database that the author specified while creating the entry which looks as following :

Offer Name
Offer Date
Offer Price
URL (which can be anything, also blabla.htm)

The URL field should then be used to create a custom individual url for each offer - for example detail.php?id=50 would become mydomain.com/offers/mallorca.htm

I know that mod_rewrite can be consulted but i haven't figured out how to combine it to use fields from my database.

Any advice?

You could rewrite as follows:

RewriteRule ^/offers/(*.)$ detail.php?url=$1

You then have to find your offer in the detail.php file by it's url and the specified url GET parameter

Just ideas for You to think of or bounce off:

  1. if You left a free hand to the author to enter the URL of their own, You would have to implement a controll for the URL uniqueness, while not letting the user to save the data until the URL part for the concrete offer is really unique
  2. easier way is to only have the URLs like mydomain.com/offers/50/ - it still looks better while the implementation tooks less time
  3. at the end, You would have to write a rewrite rule to Your .htaccess, while the result depends on what way You would go (from point 1 or point 2):
    • 1: RewriteRule ^/offers/(.*)$ detail.php?url=$1 [L, QSA]
    • 2: RewriteRule ^/offers/(.*)/$ detail.php?id=$1 [L, QSA]

If You'd go with the first option (the one You desired), You would also have to look up the offer by the URL part provided, in the second option You look up by the ID.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM