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 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了