views:

93

answers:

3

Hello, 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.

+3  A: 

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
+1 agreement. good one.
thephpdeveloper
-1 That’s nonsense. The client resolves the relative URLs in the document and not the server.
Gumbo
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
A: 

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.

thephpdeveloper
A: 

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 http://stackoverflow.com/questions/938997/problem-using-url-rewrite-relative-paths-not-working/940178#940178 and http://stackoverflow.com/questions/891775/modrewrite-url-info-required/891785#891785.

Gumbo