Custom PHP URLs from Database - php

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?

Just ideas for You to think of or bounce off:
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
easier way is to only have the URLs like mydomain.com/offers/50/ - it still looks better while the implementation tooks less time
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.

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

Related

htaccess pretty urls redirects the wrong way

I've look up for the answer to this question all around the web but I didn't find anyone with the same issue I have.
I want to create a sort of track-order like amazon or like any carrier does. To do that I assign a unique track-number to orders. Users can type the track-number into a form or directly into a url.
I get the track-number via the get method, so I need to prettify urls, but I also want to keep the search form functionality so I need a redirect.
I tried in this way:
the original url: /order/?reference=track-number where track_number is a random token like TRG58a8a132ec858.
the desired url: /order/track-number as stated above track_number is a random token like TRG58a8a132ec858.
in my .htaccess I wrote this rule:
Rewrite Engine on
RewriteRule ^/?order/([A-Z]{3}.*)/?$ /order/?reference=$1 [L,R=301]
I got some inspiration here, here, and here.
It works on reverse: when I type the original-url it does not redirect, when I type the desired-url instead it redirects to the original-url.
I am a little bit confused. Where I am making mistakes?

Need to hide id in my RewriteRule in .htaccess

I have built a custom business directory in PHP and I'm making the URLs human readable.
My URL request for getting business sub directories is:
http://.../new/business-subdirectory.php?cat=air-conditioners&cid=281
So I have written a rule in my .htaccess which gives me pretty well URL
http://.../business-directory/air-conditioners/281
Since my MySQL queries working on IDs, so I have to pass the ID (281) in the Pretty URL but I don't want it in the URL.
I just want the Pretty URL to be:
http://.../business-directory/air-conditioners/
Here is my .htaccess rule:
RewriteRule ^business-directory/(.*)/(.*)/?$ new/business-subdirectory.php?cat=$1&cid=$2
One option that I have is to use sub directories names instead of IDs... But still wanna see that I have other options or not...?
Waiting for your answers or suggestions :)
Omer
As you are performing queries based on ID passed in URL. So without changing code you can not achieve what you are looking.
Best option would be to have slug value in URL, which is unique same as your ID and you need to change your queries based on that.
so if air-conditioners is having ID 281 then you need to change your query to fetch data based on category slug and not using ID.

Remove multiple parameters and rewrite url Htaccess

I am trying to rewrite the following Url for SEO and userfriendly purposes using htaccess or other solution. I have searched and found many answers but none gives me multiple parameters and i am not advance at this.
mysite.com/myfile.php?dept=1&prodid=161&user=2018&ulevel=1&parentcust=1&prodname=My widget
to
mysite.com/products/mywidget
if My Widget has spaces or apostrophes i need them removed. example the product Tiger's eye widget should be tigerseyewidget or tigers-eye-widget
You don't have to solve your problem with rewrites only.
For starters, let's strip your query string:
user id & user level do not belong there - it's dangerous to have these in your query string. What if I change it in my browser to user=1&ulevel=5 - am I then suddenly the administrator with access to all area's? Instead, work with sessions and store the user id & ulevel there
Do you really need the product id as well as the product name in the query string? I'd say: drop the product name, the id should be enough information for you to retrieve the product name.
parentcust: what is that? Do you really need it in the query string? What if I change the value? Looks like something that belongs in a session as well.
After stripping that, all you're left with is myfile.php?dept=1&prodid=161 - now that's something that's much easier to rewrite!
How to rewrite is another question. There are plenty examples on SO how to rewrite a query string based request to a "pretty url". Changing Tiger's eye widget to tigers-eye-widget is also well documented.
But.. who generates these links? If you generate these links, why not turn it around and link your products to e.g. mysite.com/tigers-eye-widget.html and then in PHP map "tigers-eye-widget.html" to product id 161 & dept 1?
Your problem should really only apply to legacy URL's that people may have bookmarked or still appear in search results. But for those cases it's perfectly acceptable to redirect them instead of rewriting them. So you can keep the mapping in a database or textfile, process the request with PHP and do a 301 redirect to the new URL - that way you tell search engines to replace the old URL with the new one and you don't have to worry about complicated multiple .htaccess rewrite rules.

.htaccess url rewrites for white label sites

I'm building a simple site that will only have a homepage and a contact page and wondered if I could use .htaccess to rewrite the urls for different companies.
So for example if I go to website.com/companyname-contact/ it will display that url in the browser bar but actually load a generic contact.php page, I can then use php to pull in the correct contact details for that specific companyname.
This would need to work for different company names (e.g. website.com/anothercompany-contact/) but only work for an array of approved company names.
I realise this may not be possible but I thought I'd ask because i spent about 4 hours this morning Googleing it with no real progress.
Thanks
Unless you want to manually list the approved company names in your .htaccess file (which looks UGLY) I'd suggest this:
RewriteEngine On
RewriteRule (.*)-contact$ /contact.php?company_name=$1 [L,QSA,NC]
and then in your contact.php
determine if valid company name - check db or whatever method you are using. (Make sure to escape the input)
if not valid you have a couple options:
redir to your default 404 page
issue an intelligent warning page (ie include suggestions for alternate spelling that is in the db) and set a 404 header. (better IMO)
if similar company name in the db possibly redirect to that with a note at the top of the page
Yes you can. You need to enable the rewrite engine, and then you will be able to use regular expressions to accomplish what you're trying to do.
This is an example of what your htaccess could like:
RewriteEngine On
RewriteRule ^contact/([A-Za-z0-9-]+)/?$ contact.php?company=$1 [NC,L]

How to make URL with unique ID pretty and improve visibility in search results

My team is developing a new website to offer "Online tour Registration" services for a company. While designing tours page, We were discussing how to create a URL for every tour such a way that everyone can understand it and on the other hand it make sense for search engines.
So we started to analyse different ways to implement a Beautiful URL. Consider that every tour can be recognized by a unique ID, So, an example tour URL may be something like this:
http://www.example.com/tour/0x5P8btUJSo58LFh9Ukm114613052846
where 0x5P8btUJSo58LFh9Ukm114613052846 is the UNIQUE ID of the tour. The Question is, How to add Origin and Destination names is URL so everyone can understand the locations of the tour only by viewing the URL. What will you suggest? If we add origin and destination in URL, How different tours with the same destinations and origins will be declared? How adding Origin and Destination will effect on SEO and appearing in search results?
Why not put the date in the URL, too?
And you could omit the ID, if origin + destination + date is unique in your case; if not, add the departure time, too.
Various designs are possible, e.g.,:
example.com/tour/paris-to-madrid/2013-11-25
example.com/tour/paris/madrid/2013-11-25
example.com/tour/2013-11-25/paris-to-madrid/
example.com/tour/2013-11-25/paris/madrid/
I’d chose one that allows browsing by URL path manipulation. For example, if you have a list with all tours on a specific day, use the date first (example.com/tour/2013-11-25/ shows all tours on 2013-11-25); if you want to show all available tour dates for a specific tour, use the tour first (example.com/tour/paris-to-madrid/ shows all tours from Paris to Madrid); etc.
I think it’ pretty obvious that such a readable URL is more friendly than using a cryptic ID. Which link are you more likely to click, and which one gives some information before even visiting the page?
example.com/tour/paris-to-madrid/2013-11-25
example.com/tour/0x5P8btUJSo58LFh9Ukm114613052846
If you’d include the ID in addition to the readable parts, the URL would get very long, and the ID would still distract from the important parts:
example.com/tour/0x5P8btUJSo58LFh9Ukm114613052846/paris-to-madrid/2013-11-25
I'd use Apache Mod_Rewrite to rewrite all URLs of the /tour/ directory to a single script, with a query string to pass the requested path to the script.
/tours/.htaccess:
RewriteEngine On
RewriteRule ^/?tours/?(.*)$ index.php?q=$1 [NC,L]
Then, in index.php, have it handle $_GET['q']'s data in whatever format you wish. This way you can still keep any links to your current pages, but also accept the new format.
To differentiate between the different tours, I'd run a format like:
http://www.example.com/tours/Origin/Destination/TourID
Maybe make TourID a much smaller string, or even customized by the group who booked the tour.
You would need .htaccess if you're using Apache
Maybe something like
Options +FollowSymLinks
Options +Indexes
RewriteEngine on
RewriteBase /
RewriteRule ^/?tour/([a-zA-Z_]+)/([a-zA-Z_]+)$ tour.php?origin=$1&destination=$2 [NC,L]

Categories