dongzuo4666 2015-01-19 01:17
浏览 42

如何使用数据库中的别名和ID创建对人员友好的URL?

I have created a simple CMS in which each article has an id, title, alias (for URL), introtext, fulltext, etc. as:

create table articles (
    id int auto_increment primary key,
    title varchar(80),
    alias varchar(80),
    introtext text,
    fulltext text,
    published tinyint(1),
    date date
)

To access an article using the ID is easy and the URL will look something like:

www.mysite.com/article?id=31

or perhaps www.mysite.com/article?menuid=3&id=31

In my database the article looks like:

id: 3 title: This is my life alias: this-is-my-life introtext: yadda ... yadda

The goal is to take the alias field and use it to create a people-friendly URL as:

www.mysite.com/this-is-my-life.php

or

www.mysite.com/index.php?article=this-is-my-life

or

www.mysite.com/?article=this-is-my-life

In any case the article id is used to obtain the alias from the table and the alias becomes the basis of the people-friendly URL.

I see this regularly done in CMS like Joomla and Wordpress, perhaps even zen-cart. The question is how do you accomplish this with a PHP script or redirect function in the .htaccess?

New information (Jan. 23)

We seem to be talking apples to oranges as I am not understanding how this is supposed to work. Perhaps a link to a simplified demo site where you can see the menu ID structure in action will help.

http://dottedi.us/test

This is slightly more complicated than the initial example but follows the same general idea. The live demo is based upon using the joomla menu manager to create the menu item including the URL and the optional alias. it looks like:

www.dottedi.us/test/index.php?option=com_k2&view=item&layout=item&id=5

Joomla and probably the other CMS systems (Wordpress, Drupal, etc) has a global configuration parameter to enable what they call SEO-friendly URLs. I call them people-friendly. It would take the above URL and convert it to the people friendly version employing the alias field from the table so the above URL would become

www.dottedi.us/test/about.php (with or without the .php suffix)

Rather than use the link field as the visible URL I could use the alias field so that it will look like about.php when you hover over it, but I am confused how to make it return the live URL. You can see this at:

http://dottedi.us/test2

I added some debugging output which you can see if you scroll to the bottom of a page. Does this help?

  • 写回答

1条回答 默认 最新

  • doujing9972 2015-01-19 01:21
    关注

    Use mod_rewrite to create pretty URLs

    To do this, create a .htaccess file and paste this into it. Then upload it to the index of your sites root folder.

    Quick tip: You can create an .htaccess file by simply opening a text editor like Notepad (PC) or Text Edit (Mac) and then saving it as .htaccess.

    <IfModule mod_rewrite.c>
      #article rewrite
      RewriteEngine On
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteRule ^(.*)$ /article.php?id=$1 [QSA]
    </IfModule>
    

    Your URL should now look something like this:

    www.mysite.com/this-is-my-life

    INSTEAD OF:

    www.mysite.com/article?id=31

    I hope this helps!

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么