doukan5332 2013-10-31 12:28
浏览 25
已采纳

mod_rewrite RewriteRule和html链接

I'm creating a new website and it has some php in it. The site basically would work like this, i have /index.php?page=category_page. The the category would be the category and the page would be the sort-of sub category / actual page. The rewritten rule would look like this: /category/page.

I've got this:

RewriteEngine On
RewriteRule ^category/([^/]*)$ index.php?page=$1 [L]

But i dont know how to separate the category and the page, any help? The second thing is, in my index.php (for instance) i have some css from an external file:

<link rel="stylesheet" type="text/css" href="style/style.css"/>

This doesn't work with the rewrite rule, because it tries to load /category/page/style/style.css which doesn't exist / doesn't rewrite. How do i make it work? I know a simple fix would be to put /style/style.css and that would load from the root of the website, but i'm currently running the website from a sub directory e.g. example.com/new/index.php so that isn't an option. Any help with this?

  • 写回答

2条回答 默认 最新

  • dongritan5654 2013-10-31 12:29
    关注

    Problem 1:

    You could define multiple parameters in your regex, e.g. category & page (see below).

    With such a broad rewrite rule, you would want to add a condition not to rewrite for stylesheets, images, and other assets, though.

    I also modified your pattern to only match letters, digits, hyphens, and underscores, which would prevent the use of non-standard characters in your category or page names.

    RewriteEngine On
    RewriteCond %{REQUEST_URI} !\.(js|css|png|gif|jpg)$ [NC]
    RewriteRule ^([\w\d\-]+)/([\w\d\-]+)/?$ index.php?category=$1&page=$2 [L]
    

    Problem 2:

    You're using a relative url in your href, which is appending the stylesheet's location to the current location defined in your browser (/category/page/).

    Even though the server-side is rewriting that URL, the browser is unaware of the rewrite.

    If you use an absolute URL instead, your browser will define the URL relative to the BASE url (/).

    Try this:

    <link rel="stylesheet" type="text/css" href="/style/style.css"/>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来