douya1855 2011-12-02 10:59
浏览 93
已采纳

mod_rewrite重写基础

i'm working on a website, where i'd like to use mod_rewrite for rewriting URLs like

www.example.com/index.php?p=MYPAGE --> www.example.com/MYPAGE www.example.com/index.php?p=user?u=MYUSER --> www.example.com/user/MYUSER www.example.com/index.php?p=group?g=MYGROUP --> www.example.com/group/MYGROUP

I've added so far these rules.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?p=$1
RewriteRule ^user/(.+)$ index.php?p=user&u=$1

but i'm facing a problem. if i visit www.example.com/single-page everything is fine. Then if i visit www.example.com/user/SMTHING, i'm not getting to what i want. I've tried using C and L flag, without any success.

Any ideas?

Thank you

  • 写回答

4条回答 默认 最新

  • duanmengmiezen8855 2011-12-03 11:22
    关注

    No answer may look like mine, so here it is:

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^user/([a-zA-Z0-9-]+)$ /index.php?p=user&u=$1 [QSA,L]
    RewriteRule ^group/([a-zA-Z0-9-]+)$ /index.php?p=group&g=$1 [QSA,L]
    RewriteRule ^([a-zA-Z0-9-]+)$ /index.php?p=$1 [QSA,L]
    

    I'm pretty sure it works (or very close to what you want). Please tell me if it works...

    By the way how about something nicer like:

    RewriteRule ^(user|group)/([a-zA-Z0-9-]+)$ /index.php?type=$1&val=$2 [QSA,L]
    

    so in your GET you will have "type=user" or "type=group" and "val=the value". Kind of generalize stuff.

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

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程