0

Background

Trying to recreate StackOverflow-style pretty URLs using Apache's mod rewrite and PHP.

Problem

This works great:

<IfModule mod_rewrite.c>
  RewriteEngine on

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

URLs like http://localhost/home/script/5 redirect to http://localhost/home/script/index.php?r=5. The CSS content loads flawlessly.

URLs like http://24.68.226.186/recipes/recipe/5/seo-text redirect, but the relative path for the CSS files is then incorrect:

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

Directories

The directory structure for the script (index.php) resembles:

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

Question

How do you use mod rewrite to tell Apache to use the "script" directory when serving files instead of any sub-directory appended to the URL?

In other words, how would you use mod rewrite to lop off the "seo-text" part of the URL and (1) ensure it reflects the actual title [sourced from a database]; (2) redirect the browser to the new URL [if necessary]; and (3) tell Apache to ignore all paths beyond the ID in the URL?

I would rather not use absolute paths in the CSS.

Thank you!

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
  • 1
    Would using the `base` HTML tag (even if updated dynamically) be an option? (http://www.w3schools.com/tags/tag_base.asp) – Logan Bibby Apr 08 '12 at 03:53
  • Can't you set your stylesheet direct to use the root directory by adding a forward slash like this `/css/theme.css` ? – Gohn67 Apr 08 '12 at 03:54
  • The development environment has a different root than the production environment. In development it is `/home/account/htdocs/site/root` and in production it is `/home/account/site/htdocs/root`, which is why absolute paths won't work (without some effort that I'd rather avoid). – Dave Jarvis Apr 08 '12 at 19:55

1 Answers1

0

Used Logan's advice:

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

This does not work with IE8, due to a bug.

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315