Simple ModRewrite Help Needed

Discussion in 'Apache' started by joshuamc, Jan 3, 2008.

  1. #1
    I'm trying to rewrite domain.com/index.php?section=whatever and domain.com/index.php?section=whatever&page=whatever and I'm running into a snafu.

    Here's my htaccess:
    Code:

    
    Order deny,allow
    RewriteEngine on 
    RewriteRule ^section/(.+) /index.php?section=$1 [nc]
    RewriteRule ^section/(.*)/page/(.*) /index.php?section=$1&page=$2 [nc]
    
    Code (markup):

    The first rewrite works, but if there is a trailing slash, it gets put in the variable whenever I do a _GET. The second one doesn't seem to work at all. Also, I don't know that I want (.*). Maybe it'd be safer with just any numbers, letters, and underscores.

    Thoughts?
     
    joshuamc, Jan 3, 2008 IP
  2. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #2
    Options +FollowSymLinks +Indexes
    RewriteEngine on
    RewriteBase /
    RewriteRule ^section/([^.]+)/page/([^.]+)$ index.php?section=$1&page=$2 [L]
    RewriteRule ^section/([^.]+)$ index.php?section=$1 [L]
     
    Nintendo, Jan 3, 2008 IP
  3. Frankitude

    Frankitude Member

    Messages:
    32
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #3
    Try this. It is optimized for stupid Apaches :)

    
    <IfModule mod_rewrite.c>
    Options +FollowSymLinks
    RewriteEngine On
    RewriteBase /
    # only non www version
    RewriteCond %{HTTP_HOST} !^domain.com$ [NC]
    RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?section=$1
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([A-Za-z_\-]+)/([A-Za-z_\-]+) index.php?section=$1&page=$2
    </IfModule>
    
    Code (markup):
     
    Frankitude, Jan 3, 2008 IP
  4. joshuamc

    joshuamc Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks. That worked great. Now, if I wanted to remove section and page from the string so that it's domain.com/whatevera/whateverb, how would I do that?
     
    joshuamc, Jan 3, 2008 IP
  5. joshuamc

    joshuamc Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Ok, so some of this works and some doesn't Here's what I have:

    <IfModule mod_rewrite.c>
    Options +FollowSymLinks
    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?section=$1
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^/]+)/([^/]+)/?$ index.php?section=$1&page=$2
    </IfModule>
    
    
    Code (markup):
    The problem I have now is that the last rewrite isn't working right.

    I want domain.com/index.php?section=foo&page=bar to be domain.com/foo/bar.

    Right now, it's basically telling me that section=foo/bar and isn't giving the page var a value. That make sense?
     
    joshuamc, Jan 4, 2008 IP
  6. Frankitude

    Frankitude Member

    Messages:
    32
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #6
    I think you have the last line wrong.
    How about this?

    
    RewriteRule ^([A-Za-z0-9_\-]+)/([A-Za-z0-9_\-]+)$ index.php?section=$1&page=$2
    
    Code (markup):
     
    Frankitude, Jan 6, 2008 IP