普通网友 2011-04-27 21:58
浏览 46
已采纳

mod_rewrite的简单重写规则

I am in need of writing a simple mod_rewrite rule, as follows:

something.php ==> somehting (just get rid of the .php)
something_else.php ==> something/else (replace _ with /)

So, for example it would be like this:

www.mysite.com/something.php (would be rewritten as) www.mysite.com/something
www.mysite.com/something_else.php (would be rewritten as) www.mysite.com/something/else

As I am not familiar with regular expressions, I thought it would be simpler to ask someone who can do this in 5s rather than spend days (weeks? :P) learning regular expressions and mod_rewrite.

Thanks :)

  • 写回答

1条回答 默认 最新

  • duanhuangyun3887 2011-04-27 22:16
    关注

    Try these rules in .htaccess file:

    Options +FollowSymlinks -MultiViews
    RewriteEngine on
    
    RewriteRule ^([^_]+)_([^_]+)\.php/?$ /$1/$2 [R=301,L]
    RewriteRule ^([^.]+)\.php/?$ /$1 [R=301,L]
    

    This will do external redirect, in case you don't want that then remove R=301, flag from above.

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

报告相同问题?

悬赏问题

  • ¥15 怎么求交点连线的理论解?
  • ¥20 软件开发方法学习来了
  • ¥15 微信小程序商城如何实现多商户收款 平台分润抽成
  • ¥15 HC32L176调试了一个通过TIMER5+DMA驱动WS2812B
  • ¥15 cocos的js代码调用wx.createUseInfoButton问题!
  • ¥15 关于自相关函数法和周期图法实现对随机信号的功率谱估计的matlab程序运行的问题,请各位专家解答!
  • ¥15 Python程序,深度学习,有偿私
  • ¥15 扫描枪扫条形码出现问题
  • ¥35 poi合并多个word成一个新word,原word中横版没了.
  • ¥15 【火车头采集器】搜狐娱乐这种列表页网址,怎么采集?