SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2010
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using mod_rewrite with a modular file structure causes link issues

    Is anyone familiar with a modular design site and how to create links using mod_rewrite. I discovered for mod_rewrite to
    work, my links must be how they would look in the address bar like this: href=”login” instead of: href=”index.php?p=login”.
    I got that to work, but I have problems when I take it a step further and pass additional information through the address
    bar url as described below.

    includes folder
    styles.css
    footer.html
    header.html
    modules folder
    login.php
    main.php
    register.php
    .htaccess
    index.php

    RewriteRule ^([a-z]+)/?([a-z]+)?/?$ index.php?p=$1&action=$2

    index.php sandwiches together the files to display like (header.html, register.php, footer.html).
    All urls are modified by .htaccess and sent to index.php. Index.php $_GET's p=$1 and action=$2.

    The header.html has links. The links are accessible on all the pages because they're in the header.
    href=”home”
    href=”register”
    href=”login”

    If I click on register, I go to register.php. Looks good, nothing wrong here.
    http://www.mysite.com/register

    After I fill out form on register I get redirected to login: header('Location: login/jr'); exit;
    This means index.php?p=$1&action=$2 where p=login and action=jr (just registered).
    Index.php $_GET's p and action and places those values in $p and $action and properly directs me to login.php

    Now the address bar url says this: http://www.mysite.com/login/jr

    Here is where I run into a problem; NOW I click on href=”home” in the header, I am sent here:
    http://www.mysite.com/login/home instead of:
    http://www.mysite.com/home

    The link href=”home” attaches "home" after http://www.mysite.com/login/

    Is there a standard solution to this kind of problem? Am I doing this wrong? Thanks.

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,161
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Put a forward slash in front of your URLs

    href="/login"
    href="/home"
    etc.

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2010
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for responding.

    That was was the answer. It works perfectly now. I have been wrestling with this sense yesterday.

    Thanks again.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •