drllqg2903 2015-09-01 15:27
浏览 53
已采纳

mod_rewrite问题和PHP

I'm trying to convert my app links, so that a link like this:

http://localhost/index.php?id=13&category=Uncategorized&title=just-a-link

gets converted to this:

http://localhost/13/Uncategorized/just-a-test

so far I was able to do it using:

RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /index.php?id=$1&category=$2&title=$3 [QSA,L]

but that completely breaks links to css and other files as it redirects every request with query to index.php

so I changed it slightly so that it only runs when the first query is a number:

RewriteEngine On
RewriteRule ^([0-9]*)/([^/]*)/([^/]*)$ /index.php?id=$1&category=$2&title=$3 [QSA,L]

this one doesn't break css and js files, however when you go to the link for example http://localhost/13/cat/test then try to go to another link like http://localhost/19/Uncategorized/something by clicking on it inside the previous page it will instead redirect you to something like http://localhost/13/Uncategorized/19/Uncategorized/just-a-test

How do I achieve what I described without any of these weird side effects??

  • 写回答

1条回答 默认 最新

  • douzhe3516 2015-09-01 15:55
    关注

    Use :

    RewriteEngine On
    RewriteRule ^([0-9]+)/([^/.]+)/([^/.]+)/?$ /index.php?id=$1&category=$2&title=$3 [QSA,L]
    

    And add this code to your html <header>:

    <base href="/">
    

    OR

    <base href="http://www.domain.com/">
    

    To fix relative css and js links

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

报告相同问题?

悬赏问题

  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”