1

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

site.com/showCategory.php?catId=34

to:

site.com/category/34/

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
</IfModule> 

It works, but the problem is all urls (href tag) in affichage.php have "/affichage/29/" and the source for the css file becomes http://www.site.com/category/34/css/style.css.

Robert K
  • 30,064
  • 12
  • 61
  • 79

3 Answers3

3

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)(/.*)
Daren Schwenke
  • 5,428
  • 3
  • 29
  • 34
  • -1 That’s nonsense. The client resolves the relative URLs in the document and not the server. – Gumbo Oct 14 '09 at 06:53
  • 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 Oct 14 '09 at 17:33
1

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.

Community
  • 1
  • 1
Gumbo
  • 643,351
  • 109
  • 780
  • 844
0

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

e.g.:

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

  function rewritefunc($file,$params){
    $ret = '';
    switch(strtolower($file)){
      case 'affichage.php': 
        if($do_rewrite){
          $ret = 'affichage/'.$params['n'].'/';
        }else{
          $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.

mauris
  • 42,982
  • 15
  • 99
  • 131