douzuqin3467 2016-10-20 15:58
浏览 26
已采纳

mod用两个变量重写url

I want to rewrite post.php?id=1&title=test to post/1/test

I have this code:

RewriteRule ^post/([^/]+)/([^/]+)/?$ post.php?id=$1&title=$2 [L,QSA]

This only works if I rename post.php links to /post in my php file. I want to rewrite the urls so that I don't need to edit any links.

Similar to below:

RewriteCond %{THE_REQUEST} \s/+(post)\.php[?/\s] [NC]
RewriteRule ^ %1 [R=301,L]

RewriteRule ^(post)/?$ $1.php [L]
  • 写回答

1条回答 默认 最新

  • douzhuanfen5923 2016-10-20 16:07
    关注

    You can use:

    # external redirect from actual URL to pretty one
    RewriteCond %{THE_REQUEST} \s/+post\.php\?id=([^&]+)&title=([^\s&]+) [NC]
    RewriteRule ^ /post/%1/%2? [R=301,L,NE]
    
    # internal forward from pretty URL to actual one
    RewriteRule ^post/([^/]+)/([^/]+)/?$ post.php?id=$1&title=$2 [L,QSA,NC]
    

    But it is wrong to say "I don't need to edit any links". Because it is a method to correct the old links already referenced, not to prevent you from changing your code in your pages. Because HTML code still contains bad links...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用verilog实现tanh函数和softplus函数
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题