htaccess中的多个RewriteRule

时间:2012-09-11 21:26:10

标签: apache .htaccess mod-rewrite

RewriteRule ^([a-zA-Z0-9]+)/$ index.php?$1=1 [L]
RewriteRule ^([a-zA-Z0-9]+)$ index.php?$1=1 [L]

#if category 
RewriteRule ^category/([a-zA-Z0-9]+)/$ index.php?results=true&lid=$1 [L,NC]
RewriteRule ^category/([a-zA-Z0-9]+)$ index.php?results=true&lid=$1 [L,NC]

第一部分工作正常,当我在url中添加类别时,它会在其他参数到处之前写入类别,因此css和图像正在消失。

mywebsite.com/category/380应重定向到mywebsite.com/index.php?results=true&lid=380,但原始网址有pics和css,但干净的网址没有。

1 个答案:

答案 0 :(得分:0)

这是因为相对基础URI已经改变,你的css和图像可能是相对链接(例如<img src="image_folder/image.png">)而不是绝对(例如<img src="/image_folder/image.png">)。

原始基数是/,这是index.php所在的位置。但是一旦你/category/something,基数就会变成/category/。因此,您可以将所有链接从相对更改为绝对(通过添加前导斜杠),或者您可以在所有页面的标题中添加此链接:

<base href="/">

如果已经在URI前面使用/category/生成了指向css和图像的链接,那么这听起来就像是与您的框架相关的内容。但如果没有解决方法,您可以将这些规则添加到您已有的规则之上:

# if the request ends with one of these extensions
RewriteCond %{REQUEST_URI} \.(css|png|gif|jpe?g|bmp|ico|js)$ [NC]
RewriteRule ^category/(.*)$ /$1 [L]