普通网友 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.

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

报告相同问题?

悬赏问题

  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥15 ensp路由器启动不了一直报#
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 数学建模数学建模需要