1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

mod_rewrite help needed

Discussion in 'Apache' started by nsusa, May 21, 2005.

  1. #1
    I am trying to make a new website of mine more search engine friendly but are not getting the htaccess rules to work for mod_rewrite. The server has been enabled for mod_rewrite and I got some commercial scripts on other sites that work fine with htaccess / mod_rewrite.

    Sample URLs I want to convert are:

    http://www.mydomain.com/category.php?type=18
    http://www.mydomain.com/category.php?type=18&sec=42
    http://www.mydomain.com/viewlisting.php?view=6

    I tried this

    RewriteEngine On
    RewriteRule ^categories/(.*).php /category.php?type=18

    but that does not anything and I am kinda lost. Any helpful hint would be appreciated. Thanks.

    Chris
     
    nsusa, May 21, 2005 IP
  2. Evoleto

    Evoleto Well-Known Member

    Messages:
    253
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #2
    Hello Nsusa,

    First of all, hello everybody! This is my first post here!

    A correct mod_rewrite approach for URLs like:

    http://www.mydomain.com/category.php?type=18
    http://www.mydomain.com/category.php?type=18

    Are the following two .htaccess lines:

    RewriteEngine On
    RewriteRule ^categories/(.*).php /category.php?type=$1

    You may want to try also:

    RewriteEngine On
    RewriteRule ^categories/(.*)/(.*).php /category.php?type=$1&sec=$2

    That's for having http://www.mydomain.com/categories/18/42.php instead of http://www.mydomain.com/category.php?type=18&sec=42.

    It is also possible to have .htaccess disabled (from a AllowOverride rule) or mod_rewrite might not be loaded (you can check by analyzing the output oh phpinfo()).

    Good luck!
     
    Evoleto, May 21, 2005 IP
  3. sketch

    sketch Well-Known Member

    Messages:
    898
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    148
    #3
    Ah, I've been beat to it while I was writing my reply :p ... I was replying the same, but with a couple suggestions.

    I'd change the asterisk (*) to a plus (+), because asterisk means "match zero or more times", but a valid filename must have at least 1 character (plus means "match one or more times").

    I'd also change the period, because periods mean "match ANY character", which may or may not be a security hazard. I believe ([:alnum:\-\_]+) allows for all letters, numbers, and the minus (-) and underscore (_).
     
    sketch, May 21, 2005 IP
  4. nsusa

    nsusa Peon

    Messages:
    858
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hm, still does not work. What exactly would I be looking for in phpinfo?

    Chris
     
    nsusa, May 21, 2005 IP
  5. nsusa

    nsusa Peon

    Messages:
    858
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Forgot to mention - mod_rewrite is enabled on the server.

    Chris
     
    nsusa, May 21, 2005 IP
  6. nsusa

    nsusa Peon

    Messages:
    858
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Actually - it seems like maker of the script has put something into the code that causes the problem of "not working"

    $id = $rs['id'];
    						if($modrewrite=="Y"){
    							$link="category/".$id;
    						}else{
    							$link="category.php?type=".$id;
    						}
    PHP:
    He is selling his own SEO hack for the script.

    Chris
     
    nsusa, May 21, 2005 IP
  7. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #7
    If you're going to do mod_rewrite, make the URLs static, ie with no sign of .php.

    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    RewriteRule ^categories/(.*)/sec/(.*)/$ category.php?type=$1&sec=$2 [L]
    RewriteRule ^categories/(.*)/$ category.php?type=$1 [L]
    RewriteRule ^view/(.*)/$ viewlisting.php?view=$1 [L]

    domain.com/categories/WHATEVER/sec/WHATEVER/
    domain.com/categories/WHATEVER/
    domain.com/view/WHATEVER/
     
    Nintendo, May 21, 2005 IP
    TommyD likes this.
  8. harley

    harley Member

    Messages:
    45
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #8

    indeed! might even want to make them .html's and make changes so .html files are parsed by php. i'm debating about bothering with that extra part right now myself, for the added filename keyword and my theory that google treats /dir/file.html more importantly than just /dir/
     
    harley, May 21, 2005 IP
  9. sketch

    sketch Well-Known Member

    Messages:
    898
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    148
    #9
    All this talk about "categories" and "sec" ... I didn't even realize your making URLs more difficult than they need. Instead of

    RewriteRule ^categories/(.*)/sec/(.*)/$ category.php?type=$1&sec=$2 [L]

    make it

    RewriteRule ^(.*)/(.*)/$ category.php?type=$1&sec=$2 [L]

    so that instead of converting domain.com/categories/22/sec/12 you get domain.com/22/12
     
    sketch, May 21, 2005 IP
  10. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #10
    He had 'categories/' in his example, so I kept them there.

    ::RewriteRule ^(.*)/(.*)/$ category.php?type=$1&sec=$2 [L]

    The last thing you want to do is mess up the whole site. With that example, if other parts of the site had a directory and sub directory, atleast the index page would be messed up, unless maybe if you also had index.xxxx in the link. Having one word in the first part fixes that.
     
    Nintendo, May 21, 2005 IP