PHP mod_rewrites

Discussion in 'Web Design and Development' started by Dunmail, Mar 17, 2012.

  1. Dunmail macrumors regular

    Joined:
    Mar 27, 2009
    Location:
    Skipton, UK
    #1
    I'm having a bit of fun :eek: with rewrites on OS X Lion and I can't get my head around it.

    Basically I'm creating an MVC app in a subfolder of my development site (under the default Sites folder in my home folder) and the rewrite is changing this:

    PHP:
    http://localhost/~dunmail/dunmail/app/test
    to
    PHP:
    http://localhost/Users/dunmail/Sites/dunmail/test/
    When it should be

    PHP:
    http://localhost/Users/dunmail/Sites/dunmail/index/test
    If I actually type the latter in to the address bar then the correct file appears so the app side would appear to be working as far as this is concerned.

    The .htaccess is:

    Code:
    Options +FollowSymLinks
    rewriteEngine on
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
    RewriteRule ^(.*)$ index/$1 [L,QSA]
    I've changed httpd.conf and dunmail.conf to set AllowOverride All but that hasn't improved things.

    Does anyone have any clues as to what I need to modify - I'm going round in circles here!
     
  2. Yvan256 macrumors 601

    Joined:
    Jul 5, 2004
    Location:
    Canada
    #2
    Did you restart Apache? I think you need to disable then re-enable "Web Sharing" for your .htaccess changes to take effect.
     
  3. jared_kipe, Mar 17, 2012
    Last edited: Mar 17, 2012

    jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #3
    Your rule looks fishy to me. Try this. If it doesn't work, maybe .htaccess isn't being used.

    Code:
    RewriteCond %{REQUEST_URI} ^(.*)$
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule .* index/%1 [L]
    
    EDIT: After looking it up I guess I'm just more familiar with %1 vs $1 as used when its the same rule.
    I'd suspect the .htaccess file is not being used. Try what Yvan256 said, otherwise make sure Allow Overrides are turned on.
     
  4. Dunmail thread starter macrumors regular

    Joined:
    Mar 27, 2009
    Location:
    Skipton, UK
    #4
    As I said in the original post - AllowOverride is set to All. And I have restarted Apache - several times. :eek:
     
  5. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #5
    Then mod_rewrite isn't on/working.

    Or your .htaccess isn't being used.

    Put some gibberish like 'testing 12344' into the .htaccess and see if that folder gets a server error.
     
  6. Dunmail thread starter macrumors regular

    Joined:
    Mar 27, 2009
    Location:
    Skipton, UK
    #6
    I'd already got that: "The server encountered an internal error or misconfiguration and was unable to complete your request."

    However just to be sure I checked again and got the same message. Removing the erroneous text and refreshing the page removes the error.

    mod_rewrite is among the loaded modules according to phpinfo()

    Echoing $_SERVER[REQUEST_URI] from the page I'm redirecting to gives:

    /Users/dunmail/Sites/dunmail/test/
     
  7. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #7
    Ok so your .htaccess file is being used. Did you try my rule?

    What are you trying to achieve? Because the !-d looks suspicious given your input criteria.

    Are you trying to redirect or rewrite the incoming request?
     
  8. Dunmail thread starter macrumors regular

    Joined:
    Mar 27, 2009
    Location:
    Skipton, UK
    #8
    Yes, I tried "testing 1234" and got the server error message as indicated in my previous post.

    What I'm trying to do is convert a URI like "folder/test/input" in to "folder/index.php/test/input" where index.php follows the front controller pattern and in this case would route the request to the testcontroller and run the input action. I hope that makes sense.
     
  9. lucasgladding macrumors 6502

    Joined:
    Feb 16, 2007
    Location:
    Waterloo, Ontario
    #9
    I haven't done enough with custom rewriting, so this could be way off on this, but this is what I would try.

    RewriteRule ^(.*)dunmail/app(.*)$ $1Sites/dunmail/index$2 [R]

    PS: I would also use [R] while testing the rule, then change it back to the options you have when you know the rule is correct.
     
  10. Dunmail thread starter macrumors regular

    Joined:
    Mar 27, 2009
    Location:
    Skipton, UK
    #10
    I *think* I've sorted it! It looks like I needed a "RewriteBase" rule as well:

    PHP:
    RewriteBase /~dunmail/site_folder/sub_folder
    Thanks for everyone's input.
     

Share This Page