Consider a situation of a URL as:
www.example.com/softwares/download.php?f=firefox&v=13
It does not look as good as URL:
www.example.com/softwares/firefox/download?v=13
or same download.php used as:
www.example.com/softwares/chrome/download?v=20
How can I achieve this type of URL filtering in PHP?
Some point I want to covered here:
I don't need a folder-hierarchy here like having different folders for /firefox/ and different for /chrome/
There could be only two PHP files (as I wish to) for all products: /software/software-info.php and /software/download.php (already got here).
I am able to put and fetch information from database in PHP but just want to have different link for different product.
I am a Java Web Developer in which you have Filters to get information from a part of link and redirect the request accordingly.
I am new in PHP programming and if this question is already asked or obvious than please pardon me and provide that question link.
This is ideally what you should use url rewriting (mod-rewrite in .htaccess) for.
Your visitor navigates to:
www.example.com/softwares/firefox/download?v=13
or even
www.example.com/softwares/firefox/13/
but your server will understand it as:
www.example.com/softwares/download.php?f=firefox&v=13
You can use htaccess files to do URL rewriting. Essentially this would allow you to take the segments of the url after /software/ and pass them ass parameters to be controlled by the software script.
There are also a bunch of PHP frameworks which use 'routes'. They're based on a similar principal as URL rewriting. I'd recommend Codeigniter as a good starting point - it's a straightforward framework, which plenty of documentation and tutorials.
Good luck!
Related
I was looking at a few web e-commerce website and noticed that they usually have long URL something like this (taken from an e-commerce site)
https://www.example.com/bsquare-tuxedo-solid-men-s-suit/p/itmejr4zbjt6fr3h?gclid=CjwKEAiAz4XFBRCW87vj6-28uFMSJAAHeGZbXBpy4Rw5UckmBAiPaacHLhr5MbPj4bMxVThaQe5A3xoCIi7w_wcB&pid=SUIEJR4ZAK7FZKRT&cmpid=content_suit_8965229628_gmc_pla&tgi=sem%2C1%2CG%2C11214002%2Cg%2Csearch%2C%2C95089233620%2C1o5%2C%2C%2Cc%2C%2C%2C%2C%2C%2C%2C&s_kwcid=AL!739!3!95089233620!!!g!182171694500!&ef_id=WJitiQAAAfz4lw1Y%3A20170213091758%3As
Generally a router would be
http://www.example.com/controller/method/param1/param2
Then what all codes go up in the longer URL as above and why do a longer version when a short URL can get the same job done?
You can start to read more about get method:
http://www.html-form-guide.com/php-form/php-form-get.html
To send data server side in php you will need to make a request get or post. The second link is an get but rewritten.In the end to access your data the url will be rewritten by the framework you use ( http://www.example.com/controller/method?id1=param1&id2=param2 ). If you do not use any framework you will need to rewrite yourself with .htaccess Usually the second link is used for
more common pages so will be better indexed by google. The first link is for data that is more dynamic and may have more or less parameters, null or "" values etc. And also you do not care to index that page. For exempla a search : http://www.example.com/controller/method?searchValue=&page=1
You also can read more here. It explain how and why we rewrite links: https://www.smashingmagazine.com/2011/11/introduction-to-url-rewriting/
I am using this function in php
http://domain.com/page.php?do=post&id=124
but i wish to change this URL to
http://domain.com/page.php/this is first post.html
How can I change this URL?
You need to have knowledge of .htaccess to accomplish this.
What you need to achieve here is URL Rewriting.
What is "URL Rewriting"?
Most dynamic sites include variables in their URLs that tell the site
what information to show the user. Typically, this gives URLs like the
following, telling the relevant script on a site to load product
number 7.
http://www.pets.com/show_a_product.php?product_id=7 The problems with
this kind of URL structure are that the URL is not at all memorable.
It's difficult to read out over the phone (you'd be surprised how many
people pass URLs this way). Search engines and users alike get no
useful information about the content of a page from that URL. You
can't tell from that URL that that page allows you to buy a Norwegian
Blue Parrot (lovely plumage). It's a fairly standard URL - the sort
you'd get by default from most CMSes. Compare that to this URL:
http://www.pets.com/products/7/ Clearly a much cleaner and shorter
URL. It's much easier to remember, and vastly easier to read out. That
said, it doesn't exactly tell anyone what it refers to. But we can do
more:
You can find more information here.
You have to perform url rewriting. Its a technique for you you need to configure your .htaccess file.
Check this for Details
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
mod rewrite and query strings
The only way I know to pass parameters in the URL is using the GET method in PHP. But I saw many websites using what seems to be parameters directly in the URL, like this:
http://.../page/2/
In this case, is "page" really a parameter? If so, how is it handled in the code?
Or is this a regular URL of a directory "2" located in a directory "page"? Would it mean that whenever a new post is created, the website creates all the pages and the corresponding directories?
Thanks
This is called url rewriting. Basically this means that you use an apache module to rewrite incoming urls to new urls which are then handled by apache
In your example http://www.test.com/page/2/ is probably rewritten to something like http://www.test.com/?page=2.
If you search the internet for Apache URL rewrite you will get enough results explaining how you can do this.
These are all conventions. GET parameters are not specific to PHP, this is how all browsers encode form data. Java-based webapps use semicolon-separated parameters for example.
You can write a simple layer to convert ?alpha=1&beta=2 to /alpha/1/beta/2 but iw tould be just a cheap gimmick (except in a very few legitimate cases like with Squid caches).
What today's websites do is to use a single entry point pattern. Usually with apache's mod_rewrite, all requests are handled by the index.php and there is a routing facility to choose the adequate handler PHP file for a specific URL scheme. These schemes can be easily defined by Regular Expressions.
So all in all you decide how your URLs are going to look like. It is not an easy task and there are many SEO snake oil salesmen out there who will make you do all kinds of crazy stuff. What a good URL does is to identify a content (document) inside your service and you must use that single URL throughout your service to address it.
And don't forget: cool URLs don't change. You will abandon your current code base in 2 years and rebuild your site from the ground up. Design your URL scheme in a way that makes sense from a logical point of view and not something dependent on your webapp design.
The example you gave is still a GET request.
What you are looking for is URL rewriting.
I am struggling to find this as I'm not sure exactly what I'm looking for!
I've seen that some sites seem to use 'subfolders' as links to pages, e.g. wikipedia.
E.g. Let's say I had a record in a database with a unique title "My Shopping List". I want to be able to navigate to www.mySite.com/my_shopping_list and have it automatically 'forward' to a page showing the relevant record.
I am using IIS and wondering if there's a way I can do it with that, but I may be looking in the wrong place.
This is for a knowledgebase where I might write an article called "how to use a computer" and would like to be able to create a 'friendly' link for users (http://www.mySite.com/how_to_use_a_computer rather than http://www.mySite.com/article.php?ID=123. I will need to be able to create these 'links' dynamically using the title field.
Thank you
EDIT: I am using IIS6
You should use URL Rewrite, the microsoft ressource for this technique is here: http://www.iis.net/download/urlrewrite
In the iss forum the issue about url rewrite in iss < 7 is discussed with some suggestions on software to use.
I don't know which one is best, maybe there are others who know:
http://iirf.codeplex.com/
http://www.isapirewrite.com/
http://urlrewriter.net/
With PHP, you'll need to code URLConf using something like a htaccess file or the manager GUI. You can read ISS docs on URL rewrite to learn how., but as I understand, this is a module you'll have to download separately.
This question also shows how do do it without editing server configuration files or htaccess, but you'll be stuck with a http://example.com/index.php/path/to/view format (where index.php is a real script with the URLConf in it.)
Well some time ago it was used to call 'Friendly URL' or 'FURL', now i see that it's ust Rewrite URL. Look for tutorials for beginners.
Is there any method in Php by which I can create a page automatically based on a predefined template. Like if we create a new post in blogger it automatically creates page for that post with the name of that post, like this one:
http://learntoflash.blogspot.com/2009/12/exit-button-in-flash.html
Here Exit Button In Flash is the name of my post I have written and an automatic page is created for it.
Or like here on this website if we ask a question it automatically creates a page for that question. I want to know can I achieve this in Php or anything close to this ?
...here on this website if we ask a question it automatically creates a page for that question.
It sounds like you may believe an actual file is created when you post a question. My bet would be that this page is generated via the question id in the URL.
The only files created would be cached output, which may or may not resemble actual HTML pages.
You should use URL rewriting. This Apache module lets you define rules to rewrite web addresses in your desired way.
The process to make your web application ready for this, is not a short story so you should read more about it.
This article is a good starting point:
http://articles.sitepoint.com/article/guide-url-rewriting
This is acheived by using mod_rewrite. A good place to look for inspiration is the .htaccess used in Wordpress.
to something like that you have to grasp the very fundamental in php or any programming language at all, i mean the core of php is to create dynamical generated pages based on user/browser input.
You might need to take a quick tutorial about php might I suggest http://www.tizag.com/phpT/
good step for step tutorial
Edit:
if you're wondering how the websites seems to have created a html page for every question, the answer would be they're not they are probably using mod_rewrite as mentioned before to rewrite to url to print a little more user friendly url, the actual url could be something like this https://stackoverflow.com/index.php?post=4499289 in reality