Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder GSimpson's Avatar
    Join Date
    Aug 2006
    Location
    New Zealand
    Posts
    268
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Apache URL Rewrite Expression

    Hey guys. Need a little help, not going quite how I imagine it to.

    I've got an index file that opens database content based on the get variable i give it, so I'm using Apaches URL rewriting to change a directory in to that get variable, and stripping out the nasty stuff with php before it goes into the database.

    So my php for the index file is all done and working well, just need a quick tip with the rewrite code.

    At the moment it accepts both:
    http://www.example.com/home
    AND
    http://www.example.com/home/
    (forward slash difference)

    The only problem is, when I use the forward slash, it must be accepting the index.php file I direct it to as one directory further and doesn't load images and stylesheets correctly.

    Anyone have some tips on how I can redirect both to the same place that not having a forward slash sends it to?

    Here's my current .htaccess code:
    Code:
    Options +FollowSymlinks
    RewriteEngine on
    RewriteRule ^([0-9a-zA-Z+-]+)$ index.php?article=$1 [NC]
    RewriteRule ^([0-9a-zA-Z+-]+)/$ index.php?article=$1 [NC]
    There's also a little bit more in my .htaccess file about 404 pages, but I can confirm that doesn't effect it at all.

    Thank you in advance.
    The internet is my Sandbox, and notepad is my Spade n' Bucket.

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,865
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    The only problem is, when I use the forward slash, it must be accepting the index.php file I direct it to as one directory further and doesn't load images and stylesheets correctly.
    You may solve the issue by referring all the files (css,image,js etc) with absolute paths. See the post at http://www.codingforums.com/showpost...67&postcount=9

    Anyone have some tips on how I can redirect both to the same place that not having a forward slash sends it to?
    I use an alternate method, which handles all the requests(except a valid reference to any files such as image,css, js) via url to the index.php page and then serve the pages based on the value of the variable $_SERVER['REQUEST_URI']

    Say, my htaccess will have
    Code:
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_URI} !=/favicon.ico 
    RewriteRule ^(.*)$ index.php?page=$1 [L]
    and at the of index.php, there'll be something like
    PHP Code:
    $req=split('/',$_SERVER['REQUEST_URI']); 
        
    #myprint_r($req);
    if($req[1]=="")
        
    $page_='home';
    else
        
    $_page_=$req[1]; 
    (The above mentioned method has a side effect. You may not get the values from the $_GET global array as usually u do)
    Last edited by abduraooft; 06-21-2010 at 11:13 AM.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Regular Coder GSimpson's Avatar
    Join Date
    Aug 2006
    Location
    New Zealand
    Posts
    268
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Thank you for that excellent reply. I had a feeling there would be no escape from absolute paths, I was just trying my best to make it as simple as possible to create templates for. Oh well, can't win them all.

    Also, apologies for using the wrong forum, I don't remember an apache forum being there. Used to post in PHP when I first signed up. Thanks again!
    The internet is my Sandbox, and notepad is my Spade n' Bucket.


  •  

    Posting Permissions

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