duan0424 2016-05-14 02:35
浏览 144
已采纳

动态创建URL

Something that is really confusing me is how sites have urls such as:

-and-

Originally, I thought you could simply just create a php file and use URL rewrite in the .htaccess. For example:

Clearly, this is not the case as not only is it not efficient for more trailing slashes, but CMS's such as Wordpress automatically generate the content for the 'dynamic' URLs, and then redirect to a 301 page if the URL is not recognised.

I am wanting to implement this into my site, I'm just completely clueless on how I'd approach this. I am struggling to research deep into the topic due to myself being unaware what this system actually is. Obviously, I'm looking at this completely wrong, which is causing me to confuse myself.

If someone could explain to what this system is called, and how I can do it. I'd prefer not to get spoon fed code, I just need someone to explain it all too me.

EDIT: After some further researching, I have found a perfect example to make my question clearer. Notice this url:

has the same url as:

But shows different content, it's a completely new page, that is being 'dynamically' created.

Then more random URLs from the same site:

Thanks, Sutton

  • 写回答

4条回答 默认 最新

  • dongliao3742 2016-05-14 02:44
    关注

    If someone could explain to what this system is called

    It's called RewriteEngine and it's part of apache mod_rewrite, other web-servers have different mods, but this the most popular.

    RewriteEngine  on
    RewriteRule    ^shop/$  shop.php  [L]
    RewriteRule    ^shop/products/(.*?)/$  products.php?type=$1  [L]
    

    The 1st example will display the content of shop.php when a user accesses www.site.com/shop/

    The 2nd example will send games, as argument ($1), to products.php?type=$1, if a user access www.site.com/shop/products/games/


    [L] - is called a flag (L|last):

    The [L] flag causes mod_rewrite to stop processing the rule set. In most contexts, this means that if the rule matches, no further rules will be processed. This corresponds to the last command in Perl, or the break command in C. Use this flag to indicate that the current rule should be applied immediately without considering further rules.


    ^ is relative to web root (somesite.com/^)
    $ represents the end of the string (somesite.com/^somedir/test/$ this part will not be processed)


    Resources:

    Learn more about mod_rewrite and rewrite Flags

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 请问各位,如何在Jetson nano主控板的Ubuntu系统中安装PyQt5
  • ¥15 MAC安装佳能LBP2900驱动的网盘提取码
  • ¥400 微信停车小程序谁懂的来
  • ¥15 ATAC测序到底用什么peak文件做Diffbind差异分析
  • ¥15 安装ubantu过程中第一个vfat 文件挂载失败
  • ¥20 GZ::CTF如何兼容一些靶机?
  • ¥15 etcd集群部署问题
  • ¥20 谁可以帮我一下问一下各位
  • ¥15 为何重叠加权后love图的SMD与svyCreateTableOne函数绘制基线表的不一致
  • ¥150 求 《小魔指》街机游戏机整合模拟软件