duanjuda5789 2013-09-27 00:36
浏览 73
已采纳

在mod_rewrite之后重定向URL?

I'm trying to understand how the mod_rewrite works. For example, if I have a URL like:

example.com/user.php?id=123

And with the mod_rewrite I can create the URL Like:

example.com/user/123

Now this is ok, but on other locations in my application, I have linked to the user page using the old format , eg:

<a href="user.php?id=123">123</a>

Now does it mean that I would need to change all these links manually and should link according to the new format? eg:

<a href="user/123">123</a>

Or should this be done with the mod_rewrite too? I'm not using any PHP framework.

  • 写回答

2条回答 默认 最新

  • donglunzai4288 2013-09-27 00:56
    关注

    On the long run yes you would have to change it but you can also redirect that:

    1. Options +FollowSymLinks -MultiViews
    2. RewriteEngine On
    3. RewriteBase /
    4. # Externally redirect /user.php?id=123 to /user/123
    5. RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(user)\.php\?id=([^&\s]+) [NC]
    6. RewriteRule ^ /%1/%2? [R=301,L]
    7. # Internally forward /user/123 to /user.php?id=123
    8. RewriteRule ^user/([0-9]+)/?$ /user.php?id=$1 [NC,L]

    The external redirect is the one that changes the URL on the browser, the internally is the one that does not change the URL but shows the content of the other place in question.

    While the above would redirect the user from:

    example.com/user.php?id=123
    

    To

    example.com/user/123
    

    It would be for the best if you change your links to use the SEO friendly ones to avoid the extra redirect all the time a user access or navigate on your website.

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部