URL Alias uniqueness with PHP & MySQL
- Joel Lipman
- Personal Home Page
- Hits: 2868
So this is an article for me on how to copy Joomla's and Wordpress' feature where a Title/Name value is converted to a search-engine friendly URL alias and is unique in the database. Ok that's a long sentence; let me try this:
- When I save a record in JoomlaCMS or WordpressCMS
- It creates a unique name to use in URLs
- I want that
Why?
These are used as inputs to server-side scripts for the sake of search-engine friendliness. There are no silver bullets here but I want to block any character that isn't a letter, a number or an underscore.
How?
Suppose the following exists as a MySQL database table called my_table_name:
What we have:
/----------|-------------------|----------------------\ | id | name | url_alias | |----------|-------------------|----------------------| | 1 | My *First* Test | my_first_test | | | | | | | | | \----------|-------------------|----------------------/
- /----------|-------------------|----------------------\
- | id | name | url_alias |
- |----------|-------------------|----------------------|
- | 1 | My *First* Test | my_first_test |
- | | | |
- | | | |
- \----------|-------------------|----------------------/
/----------|-------------------|----------------------\ | id | name | url_alias | |----------|-------------------|----------------------| | 1 | My *First* Test | my_first_test | | 2 | My *First* Test | my_first_test_1 | | 3 | My _-_First Test | my_first_test_2 | \----------|-------------------|----------------------/
- /----------|-------------------|----------------------\
- | id | name | url_alias |
- |----------|-------------------|----------------------|
- | 1 | My *First* Test | my_first_test |
- | 2 | My *First* Test | my_first_test_1 |
- | 3 | My _-_First Test | my_first_test_2 |
- \----------|-------------------|----------------------/