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.
Related
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.
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]
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
I need to setup hundreds of short redirect URLs which follow this format:
http://mysite.com/shorturl
which will redirect to
http://mysite.com/index.php?id=N, where N is the ID corresponding to that shortcut.
What's the best way to do this.. should I be writing a ModRewrite line for each individually, or is there a better way to do this that incorporates storing a column in the database or a file?
Update: to clarify, the id N is an ID in the database. The shorturl is a small phrase used for linking people to the page corresponding to that ID.
Is there a reason why you didn't want to do something like making
http://mysite.com/shorturl
redirect to
http://mysite.com/index.php?id=shorturl
And index.php can look up the correct id in the database from the shorturl and do the right thing.
Obviously, to do this, all you'd need is one line in your .htaccess:
RewriteRule ^([A-z,0-9,_,-]+)/?$ index.php?id=$1 [PT]
is there a way to hide IDs using only .htaccess and without changing my php code?
htt://www.domain.com/show.php?categoryID=2&cname=electronic&productid=21&name=laptops
to
htt://www.domain.com/electronics/laptops
thanks in advance.
In your .htaccess you can put:
RewriteEngine On
RewriteRule ^(electronics|other|categories|here)/(laptops|other|products|here)/$ shop.php?cname=$1&name=$2
Basically I made a RegEx that matches all of your products and all of your categories, and broke that out into the cname and name and passed them to show.php.
However note I didn't pass the ID numbers to your show script. Rewrite Engine can't figure those out for you, your script will need to take the names and look up the ids itself.