SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    312
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    header.php not include css file and proper links after rewriting url with .htaccess

    Hi,
    I've a little struggle with a pretty url. Here's my .htaccess code:
    Code:
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME}.php -f  
    RewriteRule ^([^\.]+)$ $1.php [NC]
    RewriteCond %{REQUEST_FILENAME} >""
    RewriteRule ^([^\.]+)$ home/profile.php?user=$1 [L]
    and the home/profile.php
    Code:
    $getName = explode("/",$_SERVER['REQUEST_URI']);
    $result = mysql_query("SELECT userId, name FROM user WHERE name='$getName[2]' LIMIT 1");
    $num_rows = mysql_num_rows($result);
    
    if($num_rows > 0) {
    	include('header.php');
    	include('index.php');
    	include('footer.php');
    }else{
    	header('location: ../404.php');
    }
    The problems:
    1. I get the PRETTY URL I wanted but the header.php does not bring the style.css with it so it look just plain html page with no styling.
    2. When I click on the links it goes to e.g. www.mywebsite.com/photos.php instead of www.mywebsite.com/home/photos.php

    How do I solve this.

    Thanks in advance.

  2. #2
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,605
    Mentioned
    19 Post(s)
    Tagged
    2 Thread(s)
    k0,

    I can see a couple of problems with your code:

    Code:
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME}.php -f  
    RewriteRule ^([^\.]+)$ $1.php [NC]
    
    You do NOT have to escape the dot character WITHIN a character range definition thus you should not.
    It is just plain wrong to use the No Case flag against a RewriteRule (on a 'nix box - okay on WinDoze but not a good practice) because URIs ARE case sensitive. Then, you should be using the Last flag here to break the logic that would otherwise tie this RewriteRule to the next (albeit Apache ignores the implied AND in this case).[/indent] RewriteCond %{REQUEST_FILENAME} >""
    This will always be true as it includes the path to the domain which can't be null.
    RewriteRule ^([^\.]+)$ home/profile.php?user=$1 [L]
    This ASSUMES that all URIs without a dot character (file extension) will be a valid user value. IMHO, this is NOT a good assumption and will likely cause problems UNLESS your profile script has the intelligence to "dodge the bullet." To your problem, though, this RewriteRule changes directory level so all relative links will be off one level. Have a read of my signature's tutorial for more information about this as well as the two options you have to "dodge this bullet."
    PHP Code:
    if($num_rows 0) {
        include(
    'header.php');
        include(
    'index.php');
        include(
    'footer.php');
    }else{
        
    header('location: ../404.php');

    I'm not sure what else you've done (header, index and footer shouldn't be found IF they're in the home subdirectory) so this code is looking for 404 above the root directory. you can resolve this (404) problem by making it an (internal) absolute redirection (eliminate the ..).

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator


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
  •