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 problem

Discussion in 'Apache' started by sharpweb, Oct 17, 2005.

  1. #1
    I am looking to change:

    www.snowcommunity.com/wanaka/index.php
    to
    www.snowcommunity.com/location/index.php?loc=wanaka
    
    I also want it to work for:
    www.snowcommunity.com/whistler/
    to
    www.snowcommunity.com/location/index.php?loc=whistler
    
    AND
    www.snowcommunity.com/queenstown/another_folder/index.php
    to
    www.snowcommunity.com/location/another_folder/index.php?loc=queenstown
    
    AND
    www.snowcommunity.com/wanaka/another_folder/another/view.php?id=123
    to
    www.snowcommunity.com/location/another_folder/another/index.php?id=123&loc=wanaka
    Code (markup):
    So basically any subfolder of the root directory needs to be redirected to the location folder and loc=xxxx needs to be added to the end of the query string.

    I've been able to do very simple mod rewrites, but not able to get them all to work. Here is what I have so far that works for the first example only:

    
    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    RewriteRule ^(.*)/(.*)$ location/$2?loc=$1 [L]
    
    Code (markup):
    Thanks!
     
    sharpweb, Oct 17, 2005 IP
  2. sharpweb

    sharpweb Guest

    Messages:
    246
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #2
    This code works:
    RewriteEngine on
    RewriteRule ^l/(.*)/(.*)/(.*)/(.*)/(.*)$ location/$2/$3/$4/$5?loc=$1 [L]
    RewriteRule ^l/(.*)/(.*)/(.*)/(.*)$ location/$2/$3/$4?loc=$1 [L]
    RewriteRule ^l/(.*)/(.*)/(.*)$ location/$2/$3?loc=$1 [L]
    RewriteRule ^l/(.*)/(.*)$ location/$2?loc=$1 [L]
    Code (markup):
    thing is I can't figure out how to make it rewrite like this:
    snowcommunity.com/wanaka/index.php
    it adds a directory 'l' like this:
    snowcommunity.com/l/wanaka/index.php

    I can't get it to not have the extra directory....Also is there a way to avoid having a rule for each level as they are all the same...

    Thanks
     
    sharpweb, Oct 17, 2005 IP
  3. sharpweb

    sharpweb Guest

    Messages:
    246
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #3
    RewriteEngine on
    RewriteRule ^([a-zA-Z_][^\/]+)/([a-zA-Z_\/]+).php$ location/$2.php?loc=$1 [L]
    RewriteRule ^([a-zA-Z_][^\/]+)/([a-zA-Z_\/]+)/$ location/$2/index.php?loc=$1 [L]
    RewriteRule ^([a-zA-Z_][^\/]+)/([a-zA-Z_\/]+)$ location/$2/index.php?loc=$1 [L]
    RewriteRule ^([a-zA-Z_][^\/]+)/$ location/index.php?loc=$1 [L]
    RewriteRule ^([a-zA-Z_][^\/]+)$ location/index.php?loc=$1 [L]
    Code (markup):
    this is partially working, but for some reason not for all urls.

    WORKS FOR:
    http://www.snowcommunity.com/wanaka/
    http://www.snowcommunity.com/wanaka/activities/
    http://www.snowcommunity.com/wanaka/activities/snowboarding.php
    Code (markup):
    ETC

    DOEN'T WORK FOR:
    http://www.snowcommunity.com/wanaka/free_classifieds/
    http://www.snowcommunity.com/wanaka/contact_us/advertising/index.php
    Code (markup):
    ARGH i give up....need sleep...hope someone will take the time to help me out before I run out of hair to pull
     
    sharpweb, Oct 18, 2005 IP
  4. Liminal

    Liminal Peon

    Messages:
    1,279
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I find it easier to have multiple .htaccess files (one in every directory if makes sense) with each .htaccess responsible for rewriting its own files
     
    Liminal, Oct 18, 2005 IP
  5. johnt

    johnt Peon

    Messages:
    178
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Try
    
    RewriteEngine on
    RewriteCond %{REQUEST_URI} !^/location
    RewriteRule ^(^\/+)/(.*)/index.php /location/$2/index.php?loc=$1 [L,QSA]
    
    Code (markup):
    Hope it helps

    johnt
     
    johnt, Oct 18, 2005 IP
  6. sharpweb

    sharpweb Guest

    Messages:
    246
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks for the input, It's all working fine now with one exception. Here's my code so far:
    RewriteEngine on
    RewriteCond %{HTTP_HOST} !^www\..*
    RewriteCond %{HTTP_HOST}   !^$
    RewriteCond %{HTTP_HOST} ^([^.]*)\.(com|co\.uk)
    RewriteRule ^.*$     http://www.%1.%2%{REQUEST_URI} [R=permanent,L]
    
    #RewriteCond    %{REQUEST_FILENAME}  -d
    #RewriteRule    ^(.+[^/])$           /$1/  
    
    RewriteRule ^([a-zA-Z_][^\/]+)/([a-zA-Z_\/]+).php$ location/$2.php?loc=$1 [L,QSA]
    RewriteRule ^([a-zA-Z_][^\/]+)/([a-zA-Z_\/]+)/$ location/$2/index.php?loc=$1 [L,QSA]
    RewriteRule ^([a-zA-Z_][^\/]+)/([a-zA-Z_\/]+)$ location/$2/index.php?loc=$1 [L,QSA]
    RewriteRule ^([a-zA-Z_][^\/]+)/$ location/index.php?loc=$1 [L,QSA]
    RewriteRule ^([a-zA-Z_][^\/]+)$ location/index.php?loc=$1 [L,QSA]
    Code (markup):
    What i need to do is convince any url without a trailing slash to add the trailing slash and then perform the mod rewrite (see the two commented out lines). The lack of a trailing slash doesn't effect the page that I am viewing, but does effect the links on the page that don't begin with a /

    What I need it to do is add the slash, but not load the page, then perform the other rewrites. With the commented out lines it says that the page is not found because it tries to rewrite to the page that doesn't exist. (snowcommunity.com/wanaka/free_classifieds - the links on this page look like this: snowcommunity.com/wanaka/view.php but should look like: snowcommunity.com/wanaka/free_classifieds/view.php)

    Is this possible?

    Thanks!
     
    sharpweb, Oct 20, 2005 IP
  7. johnt

    johnt Peon

    Messages:
    178
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #7
    You can use the [N] flag to force the rewrite procedure to start again using the the rewritten URL as the input.
    You would need to be careful with your RewriteCond for this, as you don't want to start adding slashes to the end of files
     
    johnt, Oct 21, 2005 IP
  8. dineshnath

    dineshnath Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    what is this
     
    dineshnath, Oct 21, 2005 IP
  9. dineshnath

    dineshnath Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    i want to know about this
     
    dineshnath, Oct 21, 2005 IP
  10. dineshnath

    dineshnath Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    it is nice
     
    dineshnath, Oct 21, 2005 IP
  11. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #11
    This iz a message board. You type semi-intelligent posts once you learn about caps and periods/question marks. :D

    Take the right exit on Exit 1337. Turn left two miles down until u get 2 Leet Preschool. You will learn all you need 2 learn there.

    Oh yes. It is really nice to be in pre-school and learn about caps and periods/question marks. As a bones, tha might teach about som spalling.
     
    Nintendo, Oct 21, 2005 IP
  12. sharpweb

    sharpweb Guest

    Messages:
    246
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Thanks for your continued input John. I've managed to hack together some code that works. I think i am starting to understand these pesky mod rewrites. The first RewriteRule adds a www if its missing. The second one appends a trailing slash if that was missing. All the others deal with the loc query string variable and making the url string look nice. I'm happy with it for now....I think the other changes I am thinking of can wait for a while.
    RewriteEngine on
    RewriteCond %{HTTP_HOST} !^www\..*
    RewriteCond %{HTTP_HOST}   !^$
    RewriteCond %{HTTP_HOST} ^([^.]*)\.(com|co\.uk)
    RewriteRule ^.*$     http://www.%1.%2%{REQUEST_URI} [R=permanent,L]
    
    RewriteCond    %{REQUEST_FILENAME}  !(.*)\.(php|html|htm)
    RewriteRule    ^([a-zA-Z_\/]+[^/])$           %{REQUEST_URI}/ [R=permanent,L]
    
    RewriteRule ^([a-zA-Z_][^\/]+)/([a-zA-Z_\/]+)\.php$ location/$2.php?loc=$1 [L,QSA]
    RewriteRule ^([a-zA-Z_][^\/]+)/([a-zA-Z_\/]+)/$ location/$2/index.php?loc=$1 [L,QSA]
    RewriteRule ^([a-zA-Z_][^\/]+)/([a-zA-Z_\/]+)$ location/$2/index.php?loc=$1 [L,QSA]
    RewriteRule ^([a-zA-Z_][^\/]+)/$ location/index.php?loc=$1 [L,QSA]
    RewriteRule ^([a-zA-Z_][^\/]+)$ location/index.php?loc=$1 [L,QSA] 
    Code (markup):
    I've posted this code to help anyone that comes across this thread and for anyone to suggest a cleaner way of doing this.

    CHEERS
     
    sharpweb, Oct 21, 2005 IP
  13. sharpweb

    sharpweb Guest

    Messages:
    246
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #13
    haha I think he was drunk

    errrrr don't you mean spelling? :D
     
    sharpweb, Oct 21, 2005 IP