SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Jul 2012
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    help with my code for htaccess

    Code:
    Options +FollowSymLinks
    RewriteEngine on
    RewriteRule (.*)\.html$ fetch.php?id=$1
    I have this in my htaccess file and it works fine, but I want to know if there is a way to rewrite this so that instead of just pulling the id # i can pull the category and title up instead or will i need to do something like this

    Code:
    RewriteRule (.*)/(.*)/(.*)\.html$ fetch.php?id=$1&category=$2&title=$3
    With this method I can pull the necessary information up but the url can't be written how I want it to look. with the method above I get something very long.

    Also if this is what I need to do, how will I go about changing my title to have no spaces and only have dashes inbetween them? I heard that urls don't like spaces. I have read somewhere else they used a php code where the spaces were turned into "-" or "_" to be read into the page but reverted back to be read in sql. Let me know if you need to know more about my situation. Thanks.

  2. #2
    SitePoint Member
    Join Date
    Jul 2012
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <a href="<?php echo $row_getDisplay['post_id']; ?>/<?php echo $row_getDisplay['category']; ?>/<?php echo urlencode($row_getDisplay['title']); ?>.html">
    That is the link i'm using to call the thing.

    my htaccess looks like this now.

    RewriteRule (.*)/(.*)/(.*)\.html$ fetch.php?id=$1&category=$2&title=$3
    When I click on the link the page loads but no CSS. What could be the problem.

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

    Quote Originally Posted by jukrzy View Post
    Code:
    Options +FollowSymLinks
    RewriteEngine on
    RewriteRule (.*)\.html$ fetch.php?id=$1
    Well, I'd prefer to NEVER see (.*) (unless you mean to capture either everything or nothing) as you could use ([0-9]+) for digits or ([a-z]+) for lowercase letters - meaning that it will not match /'s, .'s, A-Z's, etc. If a hacker includes "malicious code", you won't match <script ... yadda-yadda> in your id string. Learn to code defensively so you lower the odds of a successful hack attack.

    I have this in my htaccess file and it works fine, but I want to know if there is a way to rewrite this so that instead of just pulling the id # i can pull the category and title up instead or will i need to do something like this

    Code:
    RewriteRule (.*)/(.*)/(.*)\.html$ fetch.php?id=$1&category=$2&title=$3
    
    # Why not:
    
    # RewriteRule ([0-9]+)/([a-zA-Z]+)/([a-zA-Z_\ ]+)\.html$ fetch.php?id=$1&category=$2&title=$3 [L]
    # That would REQUIRE that the if value be an integer, The value of category be one or more letters and title be one or more letters, _'s and/or spaces
    With this method I can pull the necessary information up but the url can't be written how I want it to look. with the method above I get something very long.

    Very long? Is that a concern? If so, then use a shorter category name and title! ... OR the sticky thread which was developed from an earlier version of the tutorial.

    Also if this is what I need to do, how will I go about changing my title to have no spaces and only have dashes inbetween them? I heard that urls don't like spaces. I have read somewhere else they used a php code where the spaces were turned into "-" or "_" to be read into the page but reverted back to be read in sql. Let me know if you need to know more about my situation. Thanks.
    Aw, you didn't read the tutorial article linked in my signature which used a situation very similar to this, did you?

    Quote Originally Posted by jukrzy View Post
    That is the link i'm using to call the thing.

    my htaccess looks like this now.

    Please don't use indent to indent code, PLEASE wrap your code in [code]...[/code] tags as that preserves the code when quoting for a reply.

    When I click on the link the page loads but no CSS. What could be the problem.
    As above, the answer is in the tutorial - look for "Missing Support Files."

    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

  4. #4
    SitePoint Member
    Join Date
    Jul 2012
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Dklynn, your tutorial helped a lot. I remembered reading it before and even saved it as a favorite. I guess I didn't know what I was looking for or what I wanted when I first read it. As of now I excluded my category and just left the ID and Title. I just wanted to make it more SEF instead of seeing ?id=#title=ABZ. I appreciate it. I will be going back to your tutorial as my needs may change in how I want my url shown. Thank again. I've also changed the (.*) to more proper things.

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

    Thanks for the nice comment. I try to update that tutorial every once in a while when there are new questions asked (which isn't often). Nevertheless, it should be useful as a reference (at least for the sample codes) so check back every so often (as the need arises).

    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


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
  •