duanjuda5789 2013-09-27 08:36
浏览 72
已采纳

在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 08:56
    关注

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

    Options +FollowSymLinks -MultiViews
    
    RewriteEngine On
    RewriteBase /
    
    # Externally redirect /user.php?id=123 to /user/123
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(user)\.php\?id=([^&\s]+) [NC]
    RewriteRule ^ /%1/%2? [R=301,L]
    
    # Internally forward /user/123 to /user.php?id=123
    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条)

报告相同问题?

悬赏问题

  • ¥15 对于这个复杂问题的解释说明
  • ¥50 三种调度算法报错 采用的你的方案
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败