Maintain links to certain pages after renaming
I am working on a CMS with php, mysql.
I store my page information in mysql database: table pages
columns(id
, title
, title_url
, published_time
, ... )
What is the best solution to maintain links to certain pages if a customer renames them? (title
and开发者_Python百科 title_url
change after renaming)
Example:
link to page ->homepage/gold_news
Customer renames gold_news
to gold_price_news
(now title_url
in database is gold_price_news
)
and old link to page -> homepage/gold_news
, which doesn't work anymore. You you can add a new field in the table called "redirectId"
thenn create a new row in the table with the new url (inserted id:999)
and in the old row you will set the value redirectId=999
when old urls are requested, redirect them to the new url using the http 301 status code
I would use a combination of .htaccess (mod rewrite) and the id and title fields in your DB.
Firstly, all page URLs/links should be in the form:
yourdomain.com/page/idnumber/pagetitle
With 'pagetitle' being there for plain text readability purposes and serving no purpose (merely being the title value from your DB), and the id being the actual reference to the page (the id for that page in your DB).
As you are using the id as an anchoring reference to the page, it doesnt matter if the user changes the page title/url in the DB, as the record's id is fixed.
Your .htaccess should then contain:
RewriteEngine On
RewriteRule ^page/(.*)/(.*)/?$ yourdomainpath/pagehandler.php?pageid==$1 [NC,L]
What this does is take a URL of the type specified above (e.g. yourdomain.com/page/1/my page/), and route that request to the php script which loads the content, e.g. yourdomainpath/pagehandler.php, passing the id as the GET variable 'pageid'.
The user sees a nice clean URL, which .htaccess then 'redirects' behind the scenes to your script, which takes the id out of the URL and gets the content linked to this id in your DB. As this is id based, users can have free realm to change other fields' content in the DB without any knowck on effect. A nice, clean solution.
精彩评论