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 装 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的速度时间图像)我想问线路信息是什么