Mod_rewrite OK, Ugly URL in Browser Address

Discussion in 'Apache' started by msavage, Jun 14, 2007.

  1. #1
    I've got mod_rewrite working great using this simple .htaccess:

    Options +FollowSymLinks
    RewriteEngine On
    RewriteRule ^(.+)/(.+)$ http://localhost/testsite/index.php?cat=$1&grp=$2 [L,R]

    So when I enter http://localhost/testsite/home/prod1 it gets resolved to http://localhost/testsite/index.php?cat=home&grp=prod1. My problem is the ugly URL is displayed in the browser's address field. How do I get the clean URL to display in the browser's address field?

    Thanks.
     
    msavage, Jun 14, 2007 IP
  2. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Change the URL in the destination to a path and remove the Redirect flag.

    Options +FollowSymLinks
    RewriteEngine On
    RewriteRule ^(.+)/(.+)$ index.php?cat=$1&grp=$2 [L]
     
    rodney88, Jun 14, 2007 IP
  3. msavage

    msavage Peon

    Messages:
    64
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for the help - that worked great but seems to have caused another problem.

    When I type in http://localhost/testsite/home/prod1 to my browser the page is displayed and the browser address shows the same clean URL. Perfect!

    But now my links have http://localhost/testsite/home prepended to them. So if I have a link in my HTML of page.php it shows as http://localhost/testsite/home/page.php when I really want http://localhost/testsite/page.php.

    Likewise if I type http://localhost/testsite/photos/prod2, then http://localhost/testsite/photos is prepended to all my links.
     
    msavage, Jun 14, 2007 IP
  4. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #4
    If the script is generating relative URLs, (i.e. simply href="page.php"), your browser takes these to be relative to the current directory. There's no way of knowing you're actually using mod_rewrite to rewrite these requests back up to the main directory - it assumes the links are supposed to be relative to the current (i.e. /home/) folder, even if no such folder physically exists.

    You can get around this either by editing the script to generate absolute URLs (href="http://localhost/testsite/page.php") or add a base HTML tag to your template: <base href="http://localhost/testsite/">

    As you can probably guess, that tells the browser that all links should be relative to the testsite directory, not the current directory.
     
    rodney88, Jun 14, 2007 IP
  5. msavage

    msavage Peon

    Messages:
    64
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks for all the help - I'm using absolute URLs now in my HTML and all is working OK.
     
    msavage, Jun 14, 2007 IP