user4398987
user4398987

Reputation: 35

URL rewriting messes css/js up

what I'm trying to achieve is

domain.com/register/free

that would be

domain.com/register.php?type=free

When I go go domain.com/register the page loads fine, but if I put the next /free part it cant find the css or js images etc. Not too sure where I have gone wrong but this is my .htaccess file:

RewriteRule ^register$ register.php
RewriteRule ^register/$ register.php

RewriteRule ^register/([a-zA-Z0-9\-\_]+)$ register.php?type=$1

Upvotes: 3

Views: 56

Answers (1)

Justin Iurman
Justin Iurman

Reputation: 19016

First, you can improve your rules

Options -MultiViews

RewriteEngine On
RewriteBase /

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

Then, since you create a virtual folder because of your rule (/register/) and you're using relative paths (instead of absolute paths), your links are not good anymore.

Two options (both using absolute paths):

  • add a leading slash for all your css (and js, img, etc) links.
    e.g: href="/css/style.css" instead of href="css/style.css"
  • add this tag just after <head> in all your concerned pages: <base href="/"> (this will avoid you to replace each link one by one)

Upvotes: 1

Related Questions