We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 29988
    • 89 Posts
    Just to post my actual .htaccess that I use for MODx. There are added two features
    #First, remove trailing slash, dot and comma if present. Trailing slash is well-known isuue (duplicity),
    #trailing dots, commas and so on sometimes happen in forums when URLs are wrongly parsed out
    #from a sentence. Not 100% implementation, but works fine.
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !(.*[^/,\.])$
    RewriteRule ^(.*)[/\.,]$ /$1 [L,R=301]
    
    #always add www - change your domain
    RewriteCond %{HTTP_HOST} ^tillda* 
    RewriteRule ^/*(.*)$ http://www.tillda.cz/$1 [L,R=301,QSA]
    
    #this is my experimental feature - delete it
    #if the file exists in /cache (not /assets/cache),
    #output it instead of php
    RewriteCond cache%{REQUEST_URI}.html -F
    RewriteRule .* cache%{REQUEST_URI}.html [L,QSA]
    
    #this is the standard nice url
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]


    And don’t forget to check http://www.myhtaccess.com =)
      • 6726
      • 7,075 Posts
      Thanks for sharing... could you (for the less technically minded) just explain why you needed to tweak the .htaccess, what’s the benefit of this experimental method ?
        .: COO - Commerce Guys - Community Driven Innovation :.


        MODx est l'outil id
        • 29988
        • 89 Posts
        Hi,

        .htaccess is a powerfull configuration file, where you can set and say many things very easily. It covers all the web application (directory with subdirectories) and the results are stunning - what takes you a day to create and debug in PHP, you can make by a two lines of regular expressions in .htaccess.

        Basically, Apache has many modules, they are named mod_something. In .htaccess, you can config many of these modules (if it’s applicable for the scripts), but also Apache itself (some settings).

        For example, you can specify a document, that will be shown, if the file is not found:

        ErrorDocument 404 /404.html


        One of the best known is mod_rewrite. It’s used for handling URLs, mainly for the "Cool URLs" phenomenon (see my other posts). General mod_rewrite syntax goes like this:

        RewriteCond some-condition-here
        RewriteCond another-condition-can-be-here
        RewriteRule what-url-client-wants what-he-really-gets flags
        


        So, basically, the whole thing is about: client wants someting, but we will give hime output of ANOTHER script and DON’T TELL HIM.

        Note, that in all conditions and RewriteRule parameters can be used regular expressions. If you are familiar with regexps, you know, how powerfull i can be... RewriteCond and also RewriteRule has many parameters, sitches and built in variables. Such as your ip, cookies, http referrer - and all of these you can test in RewriteCond and do thing based on them...

        MODx in default use this rule for creating cool urls:

        "If client wants www.site.com/alias and this file/directory does not exist, we will run www.site.com/index.php?q=alias and give hin output of this script instead".

        And how it is done? by three commands:

        RewriteCond %{REQUEST_FILENAME} !-f - means: follower RewriteRule will be applicable only if the file, whose name is equal to requested filename does not exist ( !-f )

        RewriteCond %{REQUEST_FILENAME} !-d - means the same with directory

        RewriteRule ^(.*)$ index.php?q=$1 [L,QSA] -the main rule, lets have a look at it:

        ^(.*)$ - regular expression. ^=begin of a string, .=any character, *=unlimited repeat and $=end of a string. Characters ( and ) means nothing, they only specify a substring for further reference. So, ^(.*)$ matches the whole string (the whole http GET argument - what the browser wants to get (what file) ).

        T index.php?q=$1 is a result, that the string that mathces will be converted to (it’s like a little bit complicated search-and-replace) and $1 means "the firs substring in ( )"... so it takes the whole string according to previous pattern, and puts it at the place of $1.

        This is just a basics. Yo can have a look at http://www.myhtaccess.com to see what you can do with .htaccess - the names of the trick will tell you.

        I hope I helped =)

        I made a big post somewhere here about SEO and the whole Cool URLS phenomen - basicly saying why cool urls are great and also mentioning some points that my .htaccess covers.

        The experimental caching lines are really for nothing - I just wasn’t happy with how modx cache works and want do some test for future development.
          • 31037
          • 358 Posts
          Great explanation, for the first time I understand how I can use .htaccess!

          Also helped me getting mod_rewrite to work locally.

          Thanks from a newbie!

          :)
            • 23265
            • 9 Posts
            thanks you, this topic is very useful
              • 7231
              • 4,205 Posts
              Great explanation, I am a bit closer to understanding .htaccess. Thank you.
                [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

                Something is happening here, but you don't know what it is.
                Do you, Mr. Jones? - [bob dylan]