Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 686
user4398987 Avatar asked Dec 13 '25 22:12

user4398987


1 Answers

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)
like image 134
Justin Iurman Avatar answered Dec 16 '25 14:12

Justin Iurman