As was foretold, we've added advertisements to the forums! If you have questions, or if you encounter any bugs, please visit this thread: https://forums.penny-arcade.com/discussion/240191/forum-advertisement-faq-and-reports-thread/

Apache mod_rewrite and 404s

SeguerSeguer of the VoidSydney, AustraliaRegistered User regular
edited January 2009 in Help / Advice Forum
Hey guys, I've been trying to rewrite everything to a single index.php file to be handled there, but my Apache throws out a 404 at /wbcms/test for example before getting rewritten. Any ideas?
RewriteCond %{HTTP_HOST} ^127.0.0.1/(.*)$ [NC]
RewriteCond $1 !(^wbcms/index.php$) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !\.(gif|jpg|png|css|js) wbcms/index.php

The first condition is for my dev area (need to rewrite differently on different hosts), the second is to stop infitnite looping (Apache stops itself at 10), and the next two were trying to combat the 404.

I adapted this from the A List Apart article, How to Succeed With URLs as well as a bunch of other resources I found.

Seguer on

Posts

  • flatlinegraphicsflatlinegraphics Registered User regular
    edited January 2009
    someone else might come in with something better, but this seems to work pretty good for me:
    RewriteRule ^inc(/)? - [S=3] 
    RewriteRule ^images(/)? - [S=2] 
    RewriteRule ^([^/.]+)(/)?$ page.php?page=$1
    RewriteRule ^([^/.]+)/([^/.]+)(/)?$ page.php?page=$1&sub=$2 [L]
    

    skips the 'inc' directory altogether, as well as the images folder. the regex pushes the requested url, with or without the trailing slash, to a variable. the site is at most 2 levels deep, so this works fine for me. so mysite.com/about/me resolves to page.php?page=about&sub=me

    do you maybe need the [L] for last rule? ie, if the rule matches, stop processing.

    granted, i don't know much, but maybe this can help?

    flatlinegraphics on
  • SeguerSeguer of the Void Sydney, AustraliaRegistered User regular
    edited January 2009
    Nope sorry, but thanks anyway. Mine can be any levels deep which is why I'm trying to get everything to be handled via php. Essentially you'd see something like example.com/projects/rewrite/how/to/make/it/work and php would start working with "/projects/rewrite/how/to/make/it/work" ($_SERVER) to determine what content to show.


    EDIT: After some testing I manage to compress the rules
    RewriteCond %{HTTP_HOST} ^127.0.0.1/(.*)$ [NC]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule !(\.(gif|jpg|jpeg|png|css|js)$|index.php$) wbcms/index.php [L]
    

    The -f and -d lines don't appear to do anything - Apache serves a 404 before attempting to apply this rule it appears, when it should be rewriting.

    Seguer on
Sign In or Register to comment.