SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    99
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    mod rewrite help

    Hey Guys and Gals,

    i have never ventured into mod_rewrite before and i have read a few tut's on them. I want to have "friendly" urls in my application and here is the rewrite i have come up with

    Code:
    RewriteEngine on
    RewriteRule ^/([a-zA-Z_]+)/([a-zA-Z_]+)(/([a-zA-Z_]+)?)(/([0-9]))?/?$ index.php?view=$1&do=$2&action=$3&id=$4 [L]
    i have done the test on this page (http://datakoncepts.com/seo) and it worked. So i know mod_rewrite is set up in my vhost file, and it well work. I am not sure what's going on. Also i hope I did the conditional ones correctly.

    Thanks

    -Colin

  2. #2
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,246
    Mentioned
    16 Post(s)
    Tagged
    0 Thread(s)
    I made two changes.

    Before
    RewriteRule ^/([a-zA-Z_]+)/([a-zA-Z_]+)(/([a-zA-Z_]+)?)(/([0-9]))?/?$ index.php?view=$1&do=$2&action=$3&id=$4 [L]

    After
    RewriteRule ^/([a-zA-Z_]+)/([a-zA-Z_]+)(?:/([a-zA-Z_]+))?(/([0-9]))?/?$ index.php?view=$1&do=$2&action=$3&id=$4 [L]

    The first was to change () to (?:). Parentheses can serve two jobs: to remember a matched value and to group values. But sometimes you want to group values without remembering the match. That's what (?:) is for. In this case, you only wanted to group the slash with the action. But because your regexp used (), then $3 would have been "/action", $4 would have been "action", and $5 would have been "id".

    The second change was to move the ? outside one more set of parentheses. Otherwise you were saying that the action is optional, but the slash preceding it is required.
    "First make it work. Then make it better."

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    99
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the help! I forgot to mention i was getting 404`s. With your version i still get 404`s

    i did a simple test (again) to make sure mod_rewrite was working here is what i added
    Code:
    RewriteRule ^test\.html$ test.php [L]
    and when i go to test.html i get what is in test.php showing. ( and this is in the same directory, so i know it's not a permissions issue).

    Any ideas why it is not been re-written?

    Thanks

    -Colin

  4. #4
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,246
    Mentioned
    16 Post(s)
    Tagged
    0 Thread(s)
    Ahh, it's possibly due to a missing quantifier on your ID pattern.

    RewriteRule ^/([a-zA-Z_]+)/([a-zA-Z_]+)(?:/([a-zA-Z_]+))?(/([0-9]+))?/?$ index.php?view=$1&do=$2&action=$3&id=$4 [L]

    If that still doesn't work, then you'll have to also show us your directory structure and the kind of URLs you're trying to use.
    "First make it work. Then make it better."

  5. #5
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    99
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    yah that still not working...

    So heres what the directory looks like:

    -Main Directory that everything sits in
    --app folder
    ---.htaccess file
    ---index.php (that calls the controllers etc)
    --- controllers


    urls i am trying to use would be

    domain/appfolder/eggs/multiAdd/

  6. #6
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,246
    Mentioned
    16 Post(s)
    Tagged
    0 Thread(s)
    You'll need to remove the first slash. Whether that slash is needed depends on whether your rewrite rule is placed in your vhost in your main configuration file, or if it's placed in a per-directory context in an .htaccess file. When you're in an .htaccess file, the directory prefix is stripped off before any processing begins.

    Also, I noticed one more parenthese group that should be non-capturing.

    Before
    RewriteRule ^/([a-zA-Z_]+)/([a-zA-Z_]+)(?:/([a-zA-Z_]+))?(/([0-9]+))?/?$ index.php?view=$1&do=$2&action=$3&id=$4 [L]

    After
    RewriteRule ^([a-zA-Z_]+)/([a-zA-Z_]+)(?:/([a-zA-Z_]+))?(?:/([0-9]+))?/?$ index.php?view=$1&do=$2&action=$3&id=$4 [L]
    "First make it work. Then make it better."

  7. #7
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    99
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks Jeff! It's working


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
  •