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条)

报告相同问题?