SEO Friendly URL - php

I love the way SO gives link to question
Like this question have the link http://stackoverflow.com/questions/6002203/seo-friendly-url
where the question title is seo-friendly-url
I'm creating a blog where i want to give the link in the same way SO do, how to do that in PHP ?
Any suggestion is welcome :)
Table Structure
ID
Title
Tags
Category
UID
Added
I'm using PHP/APACHE and no framework ! I dont want to use any blog, want to create my own

I'm not sure why people are being so deliberately obtuse here...
What you are looking for is mod_rewrite, an apache module for URL rewriting.
In your .htaccess file (you might need to make it) put:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^blog\/([0-9]+)\/.*$ /blog.php?post=$1 [L]
</IfModule>
This means when you go to /blog/10/any-old-bit-of-text/ behind the scenes it is identical to if you visited /blog.php?post=10.
The ([0-9]+) bit is called a regular expression (or regex), and matches any number. The .* means match anything. The ^ anchors to the start of the query and the $ anchors to the end. slashes (/) are escaped as \/.

First of all you didn't write what framework do you user. I describe what you want to do on Symfony framework
First solution
http://www.symfony-project.org/jobeet/1_4/Doctrine/en/05#chapter_05_route_customizations
Change routes to routing engine understand the "Nice urls"
Change controller's action which is responsible for searching the right record with your's article
You can enhance the solution by declare sluging function and use it directly in routes
Second solution
Use any blogging solution which already supports it - as wrote ceejayoz

You could use PHP and Apache together. Specifically Apache Forcetype. This article explains how to use Forcetype.
Let's say you have a URL like this: http://www.example.com/article/seo-friendly-example
The .htacess file would look like this:
<Files article>
ForceType application/x-httpd-php
</Files>
The PHP would look something like this:
<?php
list(,$slug) = explode("/", $_SERVER['REQUEST_URI']);
?>
The value of $slug would be seo-friendly-example. This would be a key in your database for that article.

Related

PHP How to avoid Question marks in my URL and use a folder like formatted link

I have created the website like WOWcrush http://phone77.ml I want the url be like phone77.ml/Naveen in title it should have my name but there come ? at the last of the URl like http://phone77.ml/?Naveen only if use ? it works fine or it shows 404 error please help
You will need to use the .htaccess file in order to point /Naveen to the coresponding get var.
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteRule ^([^/.]*)/?$ index.php?section=$1 [L]
or you can be more specific like this
RewriteRule ^/Naveen$ index.php?section=Naveen [L]
$_GET['section'] will contain Naveen and you can use that in your code to show the correct view
By default everything what goes after the '?' symbol is parameters in url. It can be changed, but for that is necessary to properly configure a web server (e.g. Apache or Nginx).
Also can be necessare to made some changes in code or/and configuration of your application.
You can see the question mark as a 'where'. So what you are saying is;
http://phone77.ml/ 'WHERE' Naveen. It all depends on how you build your site, but apparently you have a clause looking for Naveen in the URL. Maybe something like; if(isset($_GET['Naveen'])){...}.
You could also have separate documents for each you pages, then you would link to to that document instead and the URL would look like http://phone77.ml/Naveen.php f.ex.
You might also be interested in looking up the .htaccess document https://httpd.apache.org/docs/current/howto/htaccess.html where you can manipulate the URL from.

How to create custom URL user friendly?

I have a very peculiar requirement, hopefully I can explain it without being too confusing. I created a page template where I list some state's city, let's say the URL is like this: http://www.example.com/states/?q=ohio
and i would like to do it like http://www.example.com/states/ohio/
i also used add_rewrite_rule but it's does not given me output that i want.
so how could i do fix ?
It's actually not PHP, it's apache using mod_rewrite. What happens is the person requests the link, http://www.example.com/states/ohio/ and then apache chops it up using a rewrite rule making it look like this, http://www.example.com/states/?q=ohio, to the server. You can find more here: Rewrite Guide
Web server side
Since you're running Apache, you can create a RewriteRule to rewrite the URI to something that the application understands but have vanity/pretty URIs to your endusers.
For example;
RewriteEngine On
#Rewrites a request like http://www.example.com/states/ohio/ to http://www.example.com/states/?q=ohio at application level
#Having the /? at the end makes the ending slash optional
RewriteRule ^states/([^\/]+)/?$ /states/?q=$1 [L]
Tested with http://htaccess.mwl.be/
Wordpress side
You can use add_rewrite_rule to rewrite the URLs as you desire
add_rewrite_rule('^states/([^\/]+)/?$', 'states/?q=ohio$matches[1]', 'top');
Otherwise, you'd have to manually modify each href element on an anchor tag (<a />) to point to /states/ohio/ instead of /states/?q=ohio.

How to make a URL link like this http://stackoverflow.com/questions/tagged/php [duplicate]

When you edit a question on stackoverflow.com, you will be redirected to a URL like this:
https://stackoverflow.com/posts/1807421/edit
But usually, it should be
https://stackoverflow.com/posts/1807491/edit.php
or
https://stackoverflow.com/posts/edit.php?id=1807491
How was
https://stackoverflow.com/posts/1807421/edit
created?
I know that Stackoverflow.com was not created by using PHP, but I am wondering how to achieve this in PHP?
With apache and PHP, you might perform one of your examples using a mod_rewrite rule in your apache config as follows:
RewriteEngine On
RewriteRule ^/posts/(\d+)/edit /posts/edit.php?id=$1
This looks for URLs of the "clean" form, and then rewrites them so that they are internally redirected to a particular PHP script.
Quite often rules like this are used to route all requests into a common controller script, which might do something like instantiate a "PostsController" class and ask it to handle an edit request. This is a common feature of most PHP application frameworks.
It's called routing. Take a look at tutorials on the subject.
If you use a framework such as cake php it should be built in.
As #mr-euro stated you can use mod_rewrite but front controller is a far better solution.
You force every request to index.php and you write your application controlling in index.php.
You use Apache's .htaccess/mod_rewrite, and optionally a PHP file, which is the approach I like to take myself.
For the .htaccess, something like this:
RewriteEngine On
RewriteRule ^(.*)$ index.php
Then in your PHP file, you can do something like this:
The following should get everything after the first slash.
$url = $_SERVER['REQUEST_URI'];
You can then use explode to turn it into an array.
$split = explode('/', $url);
Now you can use the array to determine what to load:
if ($split[1] == 'home')
{
// display homepage
}
The array is starting from 1 since 0 will usually be empty.
It's indeed done by mod_rewrite, or with multiviews. But i prefer mod_rewrite.
First: you create a .htaccessfile with these contents:
RewriteEngine On
RewriteRule ^posts/([0-9])/(edit|delete)$ /index.php?page=posts&postId=$1&action=$2
Obvious, mod_rewrite must be enabled by your hostingprovider ;)
Using mod_rewrite this can be achieved very easily.
I am poor at this but i do know you can redirect urls using apache mod_rewrite and by touching config files. From what i remember htaccess can be used to redirect. Then internally when the user hits
http://stackoverflow.com/posts/1807421/edit it can use your page http://stackoverflow.com/edit.php?p=1807421 instead or whatever you want.
You could use htaccess + write an URI parser class.

How was a URL like http://stackoverflow.com/posts/1807421/edit created in PHP?

When you edit a question on stackoverflow.com, you will be redirected to a URL like this:
https://stackoverflow.com/posts/1807421/edit
But usually, it should be
https://stackoverflow.com/posts/1807491/edit.php
or
https://stackoverflow.com/posts/edit.php?id=1807491
How was
https://stackoverflow.com/posts/1807421/edit
created?
I know that Stackoverflow.com was not created by using PHP, but I am wondering how to achieve this in PHP?
With apache and PHP, you might perform one of your examples using a mod_rewrite rule in your apache config as follows:
RewriteEngine On
RewriteRule ^/posts/(\d+)/edit /posts/edit.php?id=$1
This looks for URLs of the "clean" form, and then rewrites them so that they are internally redirected to a particular PHP script.
Quite often rules like this are used to route all requests into a common controller script, which might do something like instantiate a "PostsController" class and ask it to handle an edit request. This is a common feature of most PHP application frameworks.
It's called routing. Take a look at tutorials on the subject.
If you use a framework such as cake php it should be built in.
As #mr-euro stated you can use mod_rewrite but front controller is a far better solution.
You force every request to index.php and you write your application controlling in index.php.
You use Apache's .htaccess/mod_rewrite, and optionally a PHP file, which is the approach I like to take myself.
For the .htaccess, something like this:
RewriteEngine On
RewriteRule ^(.*)$ index.php
Then in your PHP file, you can do something like this:
The following should get everything after the first slash.
$url = $_SERVER['REQUEST_URI'];
You can then use explode to turn it into an array.
$split = explode('/', $url);
Now you can use the array to determine what to load:
if ($split[1] == 'home')
{
// display homepage
}
The array is starting from 1 since 0 will usually be empty.
It's indeed done by mod_rewrite, or with multiviews. But i prefer mod_rewrite.
First: you create a .htaccessfile with these contents:
RewriteEngine On
RewriteRule ^posts/([0-9])/(edit|delete)$ /index.php?page=posts&postId=$1&action=$2
Obvious, mod_rewrite must be enabled by your hostingprovider ;)
Using mod_rewrite this can be achieved very easily.
I am poor at this but i do know you can redirect urls using apache mod_rewrite and by touching config files. From what i remember htaccess can be used to redirect. Then internally when the user hits
http://stackoverflow.com/posts/1807421/edit it can use your page http://stackoverflow.com/edit.php?p=1807421 instead or whatever you want.
You could use htaccess + write an URI parser class.

using seo user friendly in php

this is the URL path I currently use:
/index.php?page=1&title=articles
I want to get the URL path as
/index/page-1/title-articles
using SEO user friendly URLs in PHP.
And how to get the value of the "title"? Any one can help me pls.
Check out the mod_rewrite module, and maybe for a good starting point, this tutorial on how to take advantage of it with PHP.
You need to ensure two things:
your application prints out the new URLs properly, and
your webserver can understand that new URLs and rewrites them to your internal scheme or redirects them back to your application and your application does the rest.
The first part can be simply accomplished by using
echo ' … ';
instead of
echo ' … ';
The second part can be accomplished either with URl mapping features of your webserver (most webservers have a module like Apache’s mod_rewrite). With mod_rewrite, the following will do the rewrite:
RewriteEngine on
RewriteRule ^index/([^/-]+)-([^/]+)(.*) /index$3?$1=$2 [N,QSA]
RewriteRule ^index$ index.php [L]
The first rule will extract one parameter at a time and append it to the query. The second rule will finally rewrite the remaining /index URL path to /index.php.
I want to get the URL path as
/index/page-1/title-articles
Why? I’ve got two objections:
index is a no-information and I doubt that it belongs in the URI
page-1, as well as title-articles looks plain weird. Like with index, you should ask yourself whether this information belongs here. If it does, make clear that it’s the key of a key-value pair. Or remove it entirely.
Thus, I propose either:
/‹article›/1
or
/‹article›/page/1
or
/‹article›/page=1
or
/‹article›[1]
or
/articles/‹article›/page/1
Or any combination thereof. (In the above, ‹article› is a placeholder for the real title, the other words are literals).

Categories