This question already has answers here:
using seo user friendly in php
(3 answers)
Closed 7 years ago.
I've been wondering for quite a while how do you set up php so that you instead of getting content from tables w/ get like www.mysite.com/index.php?id=1 you get them by www.mysite.com/pages/news-1.php
I have no idea how else to make up this question; but do I have to manually create new pages and put them in directories then link the page/ID via db or is there another way with mysql only.
Create a file with name .htaccess in your web root directory. And paste these lines into it (Tested):
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^pages/news-([0-9]+)/?$ index.php?id=$1 [NC,L]
URL rewriting can be one of the best and quickest ways to improve the
usability and search friendliness of your site. It can also be the
source of near-unending misery and suffering. Definitely worth playing
carefully with it - lots of testing is recommended. With great power
comes great responsibility, and all that. Aug 4, 2008 - addedbytes.com
With URL rewriting, you need to route all page requests to your "front controller" / index.php. Then you use a router to send that request to a particular controller method. In that method, you would grab the request's path (eg. /postname). Your posts database table would have a "slug" field. You then query the database for the "posts" result that contains the slug "postname".
Please check out this tutorial to learn how to make a custom framework, it'll give you the amount of background information that might shed some light on how this all works: http://symfony.com/doc/master/create_framework/index.html
Related
This question already has answers here:
How to create friendly URL in php?
(8 answers)
SEO friendly url using php [duplicate]
(5 answers)
Closed 8 years ago.
I`ve learning development of websites recently and have appreciated quite some concepts in php and mySQL databases. Since I use GET and POST i sometimes end up with such urls: http://seta.com/news.php?articleid=231. how do i make my urls look like this instead: http://seta.com/news/today.
Maybe if one helps me on the subject so that i can search, I don`t even know what I am looking for.
This is called SEO urls or pretty urls. You can use the .htaccess file and regular expressions to rewrite the http://seta.com/news/today url to http://seta.com/news.php?articleid=231 for your program so your program can function without any modifications.
If your program in dynamically creating these links, you will have to update those places so it's outputting the urls as the new pretty urls.
Another way is to direct all urls to news.php and use $_SERVER['REQUEST_URI'] to extract the information from the url. This requires modifications to your code and it will stop working for the ?articleid=123 urls which is useful during development.
By using the GET method to submit data , a question mark followed by the submitted data will be shown like the article id for example : http://seta.com/news.php?articleid=231
<?php $article_id=$_GET['articleid']; ?>
By using the POST method , the url is not going to change :http://seta.com/news
<?php $article_id=$_POST['articleid']; ?>
i hope this is what you looking for
You need to learn some MVC php frameworks then..
CodeIgnitor is good for start
url like http://seta.com/news/today has URI Segments
where news is 1st segment and today is 2nd segment.
IN .htaccess using mode_rewrite you can remove .php from the link and create your links accordingly as you want.
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.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
.htacces to create friendly URLs. Help needed
I would like to have create a website, which loads up content from a MySQL database via PHP, while still being optimized for search engines.
I have considered a few approaches and was looking for some advice on which way would the best.
Users on my website would fill out a form and submit it, which gets saved in the SQL database. I would like to have it so that each new page could be accessed like this:
domain.com/plugins/whatever
domain.com/plugins/anotherpagename
Some approaches:
1.
A script generates a static HTML page in the plugins folder when the form is submitted. When the page is edited (there will be a edit button), the HTML page gets overwritten with the updated information.
domain.com/plugins/whatever and domain.com/plugins/anotherpagename would all redirect to a PHP script.
However, I do not know how to create a .htaccess which would allow the PHP script to get the page name so that it can search the SQL database and bring up the correct information.
I am also unsure about how search engines would treat these pages?
Maybe someone else can come up with an even better approach for this?
Thank you in advance for your help.
You have a few options for how to do this. The simplest way that matches your requirements would be to add the following to your .htaccess file:
RewriteEngine on
RewriteRule ^/plugins/(.*)$ /yourscript.php?plugin=$1 [L]
The above pass all requests to /plugins/anything to yourscript.php, from which you can access the plugin name using $_GET['plugin'].
If "plugins" is simply an example, and you want to be able to use /anything/anything - then you may wish to look at passing all requests to your PHP script, and then parsing the requested URL.
To do that, you'd use something like this in your .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /yourscript.php [L]
To break that down, the above tells Apache to rewrite all requests where the file does not exist (i.e. images/css/js, etc.) to yourscript.php.
From there, you can access the whole URL via $_SERVER['REQUEST_URI']. Which you can break down with string functions, regular expressions, and so on.
why not make an actual directory ?
Eg.
domain.com/plugins/whatever
domain.com/plugins/anotherpagename
whatever and anotherpagename would both be directories.
Then write the html file as index.html inside the directory.
No htaccess needed and you could still access using the same url as above.
I don't know if you are using any framework. I have solution using codeigniter. The same can be simulated using other frameworks / core PHP.
codeigniter specific solution:
In config/routes.php define
$route['plugins/(:any)'] = "plugins/getPageFromDb/$1";
After this, in browser the url will be same 'domain.com/plugins/whatever' but actually control will hit 'getPageFromDb' function of the 'plugins' controller with 'whatever' as the parameter.
In 'getPageFromDb' function you should write your code to get the page from db based on the parameter 'whatever' and echo the output directly or through a view template (depending on your logic / scenario).
And for performance you may keep a static version of the html page and write a relative check in the 'getPageFromDb' function to decide whether to get the page from static or from db.
Considering the url format and the well formed HTML rendering, it should be Search Engine friendly page.
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!
I'm trying to make a clean url for a blog on a dynamic website, but I think that the problem is that I don't know how to plan the website schema.
I read about how to use mod_rewrite and all I found is how to make "http://www.website.com/?category&date&post-title" to "http://www.website.com/category/date/post-title". that's works o.k for me.
The problem is that If my url looks like "http://www.website.com/blog/?id=34" this method won't work as far as I got it.
So, I have two questions:
1. Is there a way to use mod_rewrite (maybe read from a txt file) to read the post title of my blog and rewrite my url by date and post-title?
2. Should I rewrite my website to query the data from one index file in the homepage and use mod_rewrite to write the nice url? should I query also the date and the title of the post instead just the post ID?
mod_rewrite used to rewrite requests and it has nothing to do with urls. You have to change urls by hands.
yes, it's most common practice, to query the data from one index file
no, you can't use mod_rewrite to write the nice url
yes, an id must be present in the url along with title. your engine will just throw title away and use only id to retrieve an article.
Take a look at SO urls for an example
What you're talking about is commonly referred to as routing and lots of examples exist of different ways to do it with PHP. The most common approach uses the frontcontroller pattern, which means in the simple case rewriting all URLs to a single php file and then having that file determine what content to show dynamically based on the URL.
The most popular PHP frameworks (CakePHP, Symphony, Codeigniter, etc.) all have routing code in them which you might be able to use or might serve as inspiration. Alternatively this article covers lots of the basics if you want to do it yourself: http://www.phpaddiction.com/tags/axial/url-routing-with-php-part-one/
RewriteMap allows you to do all sorts of dynamic rewriting (text file, script, etc).