.htaccess rewrite if files are not existing, exepted default website

Discussion in 'Apache' started by marc_waesche, Aug 24, 2010.

  1. #1
    Hello!

    I want that my server makes a rewrite to the file 'handler.php' if a not existing file is called, i.e. [domain.com...]
    With the .htaccess code below it works generally but if the user calls the domain without the default file index.php the user is also rewrited because the apache obviously thinks this file name (without content) is also not existing.
    So, how must the code look like that it knows the domain entered without a file name is an exeption?

    Best regards
    Marc


    rewriteEngine on
    rewriteBase /
    rewriteCond %{REQUEST_FILENAME} !-f
    rewriteRule ^.* handler.php [L]
     
    marc_waesche, Aug 24, 2010 IP
  2. tolra

    tolra Active Member

    Messages:
    515
    Likes Received:
    36
    Best Answers:
    1
    Trophy Points:
    80
    #2
    The easiest way and the way I normally do it is to use index.php as the handler rather than in this case handler.php, you can do it with mod_rewrite but really I suggest renaming the file.
     
    tolra, Aug 24, 2010 IP
  3. marc_waesche

    marc_waesche Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Okay. But then I have to do a lot of changes in the index.php code. Isn't there a way to recognize a domain request (without a file name) and handle it directly in the .htaccess?

    Marc
     
    marc_waesche, Aug 24, 2010 IP
  4. tolra

    tolra Active Member

    Messages:
    515
    Likes Received:
    36
    Best Answers:
    1
    Trophy Points:
    80
    #4
    
    RewriteEngine On
    RewriteBase /
    RewriteCond %{THE_REQUEST} ^[A-Z]+\ /\ HTTP/ [OR]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule .* handler.php
    
    Code (markup):
    Should do the tick.
     
    Last edited: Aug 24, 2010
    tolra, Aug 24, 2010 IP
  5. marc_waesche

    marc_waesche Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Sorry, but it does not work. Calling the domain without the default index.php will also rewrite it.

    Marc
     
    marc_waesche, Aug 24, 2010 IP
  6. tolra

    tolra Active Member

    Messages:
    515
    Likes Received:
    36
    Best Answers:
    1
    Trophy Points:
    80
    #6
    Sorry, I've completely misread the question, note to self sleep more.

    Where do you want / to go?
     
    tolra, Aug 24, 2010 IP
  7. tolra

    tolra Active Member

    Messages:
    515
    Likes Received:
    36
    Best Answers:
    1
    Trophy Points:
    80
    #7
    Give the following a shot think this is what you're after:
    RewriteEngine on
    RewriteBase /
    RewriteCond %{THE_REQUEST} !^[A-Z]+\ /\ HTTP/
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule .* handler.php
    Code (markup):
     
    tolra, Aug 24, 2010 IP
  8. marc_waesche

    marc_waesche Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    users who call a not existing file should be rewritten to handler.php. That's all. My in my first post introduced code works so far but does also rewrite users who call the domain without the default file (index.php) what of course the most users do not enter.
     
    marc_waesche, Aug 24, 2010 IP
  9. marc_waesche

    marc_waesche Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    YEAAAAH!! That's it! Thanks a lot, my friend!!

    Marc
     
    marc_waesche, Aug 24, 2010 IP
  10. romika

    romika Member

    Messages:
    264
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #10
    romika, Aug 24, 2010 IP
  11. marc_waesche

    marc_waesche Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Thank you, Romika! I will keep it for further problems. I hope so far in the future that computers are programming our servers. ;)

    Marc
     
    marc_waesche, Aug 24, 2010 IP