drllqg2903 2015-09-01 07: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:

  1. RewriteEngine On
  2. 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:

  1. RewriteEngine On
  2. 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 07: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

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部