SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2006
    Location
    Dubrovnik, Croatia
    Posts
    82
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    .htaccess - Rules order and last as default

    Hi,

    I have problem with my .htaccess file. The idea is to have site http://www.test.com/ and urls like http://www.test.com/en/test.html or
    http://www.test.com/en/article/new-year.html

    Here is my .htaccess

    Code:
    Options +FollowSymLinks +ExecCGI
    RewriteEngine On
    
      # we check if the .html version is here (caching)
      RewriteRule ^$ index.html [QSA]
      #RewriteRule ^([^.]+).html$ $1.html [QSA]
      RewriteCond %{REQUEST_FILENAME} !-f
    
    RewriteRule ^([de|fr|en|ru|es|hr]+)$ index.php?lang=$1 [L]
    RewriteRule ^([de|fr|en|ru|es|hr]+)/$ index.php?lang=$1 [L]
    RewriteRule ^([de|fr|en|ru|es|hr]+)/([^.\/]+).html$  index.php?lang=$1&module=page&page1=$2 [L]
    RewriteRule ^([de|fr|en|ru|es|hr]+)/([^.\/]+)/([^.\/]+).html$  index.php?lang=$1&module=page&page1=$2&page2=$3 [L]
    RewriteRule ^([de|fr|en|ru|es|hr]+)/([^.\/]+)/([^.\/]+)/([^.\/]+).html$  index.php?lang=$1&module=page&page1=$2&page2=$3&page3=$4 [L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ error_page.php?opt=$1 [QSA,L]
    Evrything seems to be working but apache loads error_page.php allways i don't know why. This last rule is here for "if none of the rules matches use this one as last in order". And this works, if he can't find he loads error_page.php but even if he has rule above he loads error_page.

    Can someone help or give me guidance for this prob. Thnx!

  2. #2
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,672
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    dj,

    While I will normally go from most specific to most general when ordering the mod_rewrite block statements, it appears that your code is good enough not to be affected (despite the fact that you should not have \ in the range definitions and should escape the dot characters NOT in the range definitions).

    Your final block is apparently what's causing the problem, though as it's redirecting everything which was not originally requested ({REQUEST_FILENAME}) and not existing as a file to error_page.

    The simple way to work around this ({REQUEST_URI} changes as you go through mod_rewrite redirects while {REQUEST_FILENAME} apparently does not) is to use Apache's core statement ErrorDocument 404 error_page.php and have it look at the {REQUEST_FILENAME} rather than do that via mod_rewrite.

    As a rule of thumb, if you have a better tool than mod_rewrite, use it!

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 2006
    Location
    Dubrovnik, Croatia
    Posts
    82
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's a great idea with Apache ErrorDocument.

    Thank you


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
  •