RewriteRule删除漂亮URL的无关URL部分

时间:2012-04-08 02:56:43

标签: php .htaccess mod-rewrite pretty-urls

背景

尝试使用Apache的mod重写和PHP重新创建StackOverflow风格的漂亮URL。

问题

这很有效:

<IfModule mod_rewrite.c>
  RewriteEngine on

  RewriteRule ([0-9]+)$ index.php?id=$1 [L]
</IfModule>

http://localhost/home/script/5等网址重定向到http://localhost/home/script/index.php?r=5。 CSS内容加载完美。

http://24.68.226.186/recipes/recipe/5/seo-text重定向的网址,但CSS文件的相对路径不正确:

<link rel="StyleSheet" hreF="css/theme.css" type="text/css" media="screen,print" id="css-theme" />

目录

脚本的目录结构(index.php)类似于:

htdocs/home/script
htdocs/home/script/.htaccess
htdocs/home/script/index.php
htdocs/home/script/css
htdocs/home/script/images

问题

如何使用mod重写告诉Apache在提供文件而不是附加到URL的任何子目录时使用“script”目录?

换句话说,你如何使用mod重写来删除URL的“seo-text”部分;(1)确保它反映实际的标题[来自数据库]; (2)将浏览器重定向到新URL [如有必要]; (3)告诉Apache忽略URL中ID以外的所有路径?

我宁愿不在CSS中使用绝对路径。

谢谢!

1 个答案:

答案 0 :(得分:0)

使用Logan的建议:

<base href="/home/script/" target="_self" />

由于存在错误,这不适用于IE8。

相关问题