I have php application which have to be partially rewritten because of the customer request to have SEO frendly urls.
My links are as follow:
www.mysite.com/articles_en.php?artid=89, where I will have to change the url in this:
www.mysite.com/articleTitle
Then I have this url:
www.mysite.com/halls.php?fairid=65 which should become
www.mysite.com/fairname
And www.mysite.com/companies.php?fairid=65&hallid=23 which should become
www.mysite.com/fairname/hallname
You get the idea.
I need a help with the approach. Is it good idea in the tables of the fairs, halls and articles to create a field in the table named for example alias and then to attempt to rewrite the url? Anyone can help me with the steps how to create this script, or to point me to better approach?
I am good at php, but not good at all with regular expressions, so I will be lost on the .htaccess part.
Any help will be deeply appreciated.
Regards, Zoran
.htaccess something like:
database table something like:
With the path as the PRIMARY KEY.
And a PHP script called
url_rewrite.php
that takes the path parameter from the URL, does a lookup on the database to find the real url and performs an HTTP 301 redirect.The url_rewite.php page might look something like this (this is pretty much boilerplate code and will need adjusting to fit your requirements - realistically we shouldn't be using mysql_query() any more either - PDO or MySQLi are better and aren't well, deprecated).
I suggest you to carefully look at the pretty URL of this question on SO and get some idea. Needless to add that SO questions rank very high on Google search results. So taking a clue from SO URL conventions, I suggest you these 3 pretty URL formats:
/articles_en.php?artid=89
/halls.php?fairid=65
/companies.php?fairid=65&hallid=23
Now create 3 lookup tables
articles
,halls
andcompanies
like this:Table: articles:
Table halls:
Table companies:
Now for above 3 pretty URL handling add this code in your .htaccess under
$DOCUMENT_ROOT
:Finally have your
router.php
code like this: (sample code)Once you verify that it's working fine uncomment
301 Moved Permanently
lines to get better SEO results.PS: I have used
normalize
PHP function to get all URL text in lowercase, cleaning up special characters and converting all spaces to hyphens.