How to prevent rewriting href attributes when rewriting urls?

on my web site i want to change url's from (just exemple):


Here is the contents of my .htaccess file:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^affichage/([0-9]+)$ /affichage/$1/ [R]
    RewriteRule ^affichage/([0-9]+)/$ /affichage.php?n=$1

It works, but the problem is all urls (href tag) in affichage.php have "/affichage/29/" and the source for the css file becomes

14.10.2009 00:53:05

I add this to exclude css and other files I want directly accessible:

RewriteCond  %{REQUEST_FILENAME} !\.(php|ico|png|jpg|gif|css|js|gz|html?)(\W.*)?

Or, to exclude by directory name:

RewriteCond  %{REQUEST_FILENAME} !^/(css|js|static|images)(/.*)
14.10.2009 01:03:31
-1 That’s nonsense. The client resolves the relative URLs in the document and not the server.
Gumbo 14.10.2009 06:53:17
It depends on what his issue really is here. If he has a common css he needs to include no matter where he is relative to /, this is the way to do it. That's my most common scenario.
Daren Schwenke 14.10.2009 17:33:47

You will have to write the URL out again. However good practice is to wrap it with a function.


<a href="<?php rewritefunc('affichage.php',array('n'=>29)); ?>">This URL</a>
  $do_rewrite = true;

  function rewritefunc($file,$params){
    $ret = '';
      case 'affichage.php': 
          $ret = 'affichage/'.$params['n'].'/';
          $ret = 'affichage.php?'.http_build_query($params);
    return $ret;

Using the switch statement you can add more url parsing. Also with this wrapper function, you can easily switch between old and new URLs.

14.10.2009 01:08:45

This is a URL resolving issue. You need to use absolute URLs (or at least absolute URL paths) when referencing your external resources. Use the absolute URL path /css/… instead of a relative css/… or ./css/….

Or you change the base URL the relative URLs are resolved from with the BASE HTML element. But note that that will affect all relative URLs.

See also Problem using URL rewrite (Relative Paths not working) and mod_rewrite URL info required.

23.05.2017 10:27:38