Menu

Case insensitive apache server

Webmin
2010-01-14
2013-05-22
  • nate faulds

    nate faulds - 2010-01-14

    does anyone know what i need to do to make my apache server so that its case insensitive, so /test could go to /TEST, etc, i cant find a way, i enable mod_spelling, but its not working, someone help me out please.

    Thanks

     
  • James Butler

    James Butler - 2010-01-14

    Case-sensitivity is controlled by the operating system (Unix, Windows, etc.) and not by the applications that run on them.

    That being said, you could investigate Apache's 'mod_rewrite' module (http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html) and perhaps its 'nocase|NC' flag. It's a big row to hoe, but maybe your situation would be manageable.

    James

     
  • nate faulds

    nate faulds - 2010-01-14

    i have modrewrite enabled to, what you mean NC' flag?? i just need a quick and easy way to have case insensitive URL's

     
  • James Butler

    James Butler - 2010-01-14

    See the documentation I linked to. All mod_rewrite answers can be found there.

    Here's an example of a simple conditional test using the NC flag (this would need to be followed by a RewriteRule):

    RewriteCond  %{REQUEST_URI}  ^test.* [NC]
    

    Also consider simple substitution (upper or lowercase request goes to lowercase directory):

    RewriteRule  ^/(test|TEST)/(.*)$   /test/$1  [L]
    

    You would need one rule for every instance where upper and lowercase might be swapped. A better idea is to use only lowercase in your naming conventions, and then force everything to be a lowercase request. See my final comment, below.

    More complicated to understand but more likely to satisfy your needs, if you use consistent naming conventions, is the RewriteMap directive using Apache's internal 'tolower' function:

    RewriteMap makelc int:tolower
    RewriteRule ^(.*) ${makelc:$1} [NC,L]
    

    Note that last bit is untested, but expresses the general 'map' idea to make the entire request lowercase. Check the documentation and brew a few recipes of your own. As long as you try one at a time, you should be able to recover from any serious missteps. (Of course, experiment on a test server, and not on a live production server. ;) )

    Lastly, since you are running a case-sensitive operating system, my humble suggestion would be to keep all file and directory names lowercase all the time so this type of thing is more simple to resolve. If you have to keep track of which files and directories use uppercase letters, you're setting yourself up for a lifetime of PITA.

     
  • nate faulds

    nate faulds - 2010-01-14

    ummmm yea, im not much of a apache settings coder or anything, im running ubuntu server.

    could you give me instructions on what to add and what to add it to (which config httpd.conf or apache.conf etc…)
    im a very dumb server admin with linux OS's.

    If you could help i cant thankyou enough.

     
  • James Butler

    James Butler - 2010-01-14

    Long ago, in a galaxy far, far away, I, too, was a newbie. :) So here we go …

    1) Before you start messing with mod_rewrite, take however long it takes to "correct" your website files and directories by making their names ALL lowercase, all the time … and no spaces, either! (Use under_scores or hy-phens instead of spaces ALWAYS.) Once you're done with mod_rewrite, it won't matter what the links look like … we'll be converting all requests to lowercase, anyway. But the actual file and directory names DO need to be REALLY all lowercase, or else the rewrite won't match and you'll get "404 File not found" errors.

    2) mod_rewrite instructions are instructions used by Apache, so there are basically two places where they can be placed: Where Apache can grab them when it starts up, and where it can grab them when it is satisfying a request.

    The first place (start up) is Apache's configuration file: httpd.conf

    The second place (per request) is in the directory where the instructions apply: using a special file named .htaccess

    For simplicity, I am going to guide you toward using the Apache configuration file, httpd.conf, which will affect the entire domain in whose configuration you include these instructions. If you decide you only want to apply this stuff in one or two directories, and NOT to the entire domain, you'll need to read up on using .htaccess files, and then simply apply this same info to that implementation. (You can use this same stuff in either httpd.conf or .htaccess without changing it. WHERE you apply this stuff simply determines how much of the domain's files are affected … the whole lot or just a directory or two.)

    3) AFTER you have "corrected" all of your files and directories to be all lowercase (go ahead … I'll wait) …

    4) Open httpd.conf in a text editor. I'm going to use my own server's path (/etc/httpd/conf) and the programs I like to use (vi) for these instructions. Swap out your own path and program information. You could also use FTP or something to download httpd.conf, modify it and then upload it. I usually like to do these kinds of things via telnet/SSH.

    vi /etc/httpd/conf/httpd.conf
    

    5) Locate the instruction set for the domain you want to apply this to.

    As you will notice, a set of instructions for a domain is similar to HTML, in that there is an "ON" tag (<Directory…> or <VirtualHost…>) and an "OFF: tag (</Directory> or </VirtualHost>). We are going to add instructions WITHIN those ON and OFF tags.

    Using 'example.com' as an example, and some fake instructions aside from mod_rewrite instructions, here is a VERY basic sample:

    <Directory "/var/www/html">
     Options FollowSymLinks
     AllowOverride None
     Order allow,deny
     Allow from all
    </Directory>
    

    (Keep your eye on that "AllowOverride None" instruction. If you decide to use .htaccess files in specific directories instead of modifying httpd.conf, you will need to change your AllowOverride value in httpd.conf to allow Apache to use the .htaccess file in that Directory … which would let it "override" whatever is in httpd.conf. See? Nice and easy! ;) )

    6) Between the ON and OFF tags, add your mod_rewrite instructions:

    <Directory "/var/www/html">
     Options FollowSymLinks
     AllowOverride None
     Order allow,deny
     Allow from all
     RewriteEngine on
     RewriteMap makelc int:tolower
     RewriteRule ^(.*) ${makelc:$1} [NC,L]
    </Directory>
    

    Here's what this example is doing, line by line:

    a) For "Directory '/var/www/html'" (main website root directory),
    b) activate the FollowSymLinks option (follow symbolic links in that directory),
    c) do NOT allow anything in that directory to override httpd.conf,
    d) first trust, then do not trust, all requests (don't bother looking for any type of authorization)
    e) but basically let anyone who asks make a request without further authorization,
    f) turn on the mod_rewrite program in this directory,
    g) use Apache's internal (int) 'tolower' mapping function, which we are assigning to the variable named 'makelc',
    h) and rewrite ALL requests made from this directory:
    from the start of each request line (^) grab everything (.* = regular expression: unlimited number  of any character ) into a match container (the parenthesis form the container) then apply the function stored in the 'makelc' variable (${makelc:…}) to whatever is in that match container ($1 = the first container - we only have the one in this example), ignoring the request's case (NC) and setting this rule as the last rule (L) (so just do it, already), to turn whatever was matched ((.*)) to all lowercase before trying to satisfy the request.
    i) then finally close out the instruction set for that directory (</Directory>)

    Once this has been added, save the file and restart Apache so it can read in the new instructions. If you are using an .htaccess file, instead, you still need to modify the AllowOverride value for that directory in httpd.conf, save it and restart Apache before your .htaccess file will be used.

    That should be it. Again, though, I have not tested this bit of rewrite code, specifically. As long as everything is really all lowercase, then every request from that Directory, whatever its case, should be rewritten to an all lowercase request before Apache tries to find the file.

    For example:

    example.com/TEST/TeStPaGe.HTml => example.com/test/testpage.html

    One last note: part of the joy of becoming a non-newbie is in the learning. Part of the learning is this process, where someone who can spells things out for you to absorb. The other, far more valuable part is the process of trying to work out the code for yourself. I know it seems like too much stuff at the moment, and it DOES take a lot of time, but reading the documentation and then running an experiment or two is really the best way to learn how to code, once you have the basics in hand. (Not disparaging CompSci people … there is definitely something to be said for learning systems and protocols from the Masters, too.) You should get comfortable with experimenting and working your way through documentation if you plan on doing any significant web stuff in the future. Otherwise, you will be dependent on others for help, and that only works when others CAN help, which is not much good when the server goes down at 3:AM and your boss is on your cell screaming bloody murder. Have fun! :)

     
  • James Butler

    James Butler - 2010-01-14

    PS: This is the Webmin forum … so I'm going to stop commenting on Apache stuff. Sorry everyone! Hope it was useful!

     
  • James Butler

    James Butler - 2010-01-14

    …and lastly (whew) … to be clear …

    RewriteEngine, RewriteMap and RewriteRule go in EITHER httpd.conf OR .htaccess, not both.

    IF you use httpd.conf, do it like I showed. IF you decide to use .htaccess, instead, put the Rewrite… stuff in .htaccess and REMOVE "AllowOverride None" from that Directory's instruction set in order to let the .htaccess file override the httpd.conf file. (Google "allowoverride .htaccess" for more.)

     
  • nate faulds

    nate faulds - 2010-01-15

    Well im using webmin to do all of this so i guess this counts, i just need some help, anyway, after i ypdate the config with the config you listed, i get this error:

    Syntax error on line 7 of /etc/apache2/httpd.conf:
    RewriteMap not allowed here
    
     
  • James Butler

    James Butler - 2010-01-15

    Doh!

    Just place the RewriteMap line outside of the <Directory…> instructions, so that it becomes a server instruction:

    RewriteMap makelc int:tolower
    <Directory "/var/www/html">
     Options FollowSymLinks
     AllowOverride None
     Order allow,deny
     Allow from all
     RewriteEngine on
     RewriteRule ^(.*) ${makelc:$1} [NC,L]
    </Directory>
    

    RewriteMap apparently only applies to server configuration or virtual host configuration, not directory configuration. Sorry 'bout that.

    (Error on Line 7?? Wow. That's really early on in your httpd.conf. My Directory instructions are way down in the 100+ line range. Are you modifying an existing Directory instruction set?)

     
  • nate faulds

    nate faulds - 2010-01-15

    its a new server im setting up, dont have alot configed right now, need to get SSL setup, and figure out why my .htaccess files wont load in apache whenever someone goes to a directory that has one in it, but on this subject, should i see a the config working immediately?? cause its not for me

     
  • James Butler

    James Butler - 2010-01-15

    This will work immediately upon server restart, if it works at all. That is to say … if the Apache module is loaded BEFORE the instructions that use it and IF the module is functional, which it would be if it was compiled for your version of Apache, stored in the proper location and referenced properly in the httpd.conf file.

    From the "Line 7" thing, it sounds like there are some significant issues with your configuration. Even on a brand new server I have never had an httpd.conf file that was so short. The comments take up more lines than that! Typically there are many things that need to be initialized before the Directory instructions can be set.

    If your httpd.conf is really that short, and if the Webmin fans don't mind, please post your httpd.conf. If it is a lot longer than 7 lines (like in the normal hundreds of lines), we have other things to talk about … ;)

     
  • James Butler

    James Butler - 2010-01-15

    At the very least, can you use Webmin's Others => Command Shell to execute the following command, and paste its output here:

    httpd -l

    (That's 'httpd -ell', to 'list' all of the compiled-in modules.)

     
  • nate faulds

    nate faulds - 2010-01-15

    well i agree, i havent had a short httpd.conf, the config file that looks like httpd.conf should is my apache2.conf file.

    Heres my apache2.conf file:

    #
    # Based upon the NCSA server configuration files originally by Rob McCool.
    #
    # This is the main Apache server configuration file.  It contains the
    # configuration directives that give the server its instructions.
    # See http://httpd.apache.org/docs/2.2/ for detailed information about
    # the directives.
    #
    # Do NOT simply read the instructions in here without understanding
    # what they do.  They're here only as hints or reminders.  If you are unsure
    # consult the online docs. You have been warned.  
    #
    # The configuration directives are grouped into three basic sections:
    #  1. Directives that control the operation of the Apache server process as a
    #     whole (the 'global environment').
    #  2. Directives that define the parameters of the 'main' or 'default' server,
    #     which responds to requests that aren't handled by a virtual host.
    #     These directives also provide default values for the settings
    #     of all virtual hosts.
    #  3. Settings for virtual hosts, which allow Web requests to be sent to
    #     different IP addresses or hostnames and have them handled by the
    #     same Apache server process.
    #
    # Configuration and logfile names: If the filenames you specify for many
    # of the server's control files begin with "/" (or "drive:/" for Win32), the
    # server will use that explicit path.  If the filenames do *not* begin
    # with "/", the value of ServerRoot is prepended -- so "/var/log/apache2/foo.log"
    # with ServerRoot set to "" will be interpreted by the
    # server as "//var/log/apache2/foo.log".
    #
    ### Section 1: Global Environment
    #
    # The directives in this section affect the overall operation of Apache,
    # such as the number of concurrent requests it can handle or where it
    # can find its configuration files.
    #
    #
    # ServerRoot: The top of the directory tree under which the server's
    # configuration, error, and log files are kept.
    #
    # NOTE!  If you intend to place this on an NFS (or otherwise network)
    # mounted filesystem then please read the LockFile documentation (available
    # at <URL:http://httpd.apache.org/docs-2.1/mod/mpm_common.html#lockfile>);
    # you will save yourself a lot of trouble.
    #
    # Do NOT add a slash at the end of the directory path.
    #
    ServerRoot "/etc/apache2"
    #
    # The accept serialization lock file MUST BE STORED ON A LOCAL DISK.
    #
    #<IfModule !mpm_winnt.c>
    #<IfModule !mpm_netware.c>
    LockFile /var/lock/apache2/accept.lock
    #</IfModule>
    #</IfModule>
    #
    # PidFile: The file in which the server should record its process
    # identification number when it starts.
    # This needs to be set in /etc/apache2/envvars
    #
    PidFile /var/run/apache2.pid
    #
    # Timeout: The number of seconds before receives and sends time out.
    #
    TimeOut 300
    #
    # KeepAlive: Whether or not to allow persistent connections (more than
    # one request per connection). Set to "Off" to deactivate.
    #
    KeepAlive on
    #
    # MaxKeepAliveRequests: The maximum number of requests to allow
    # during a persistent connection. Set to 0 to allow an unlimited amount.
    # We recommend you leave this number high, for maximum performance.
    #
    MaxKeepAliveRequests 100
    #
    # KeepAliveTimeout: Number of seconds to wait for the next request from the
    # same client on the same connection.
    #
    KeepAliveTimeout 15
    ##
    ## Server-Pool Size Regulation (MPM specific)
    ## 
    # prefork MPM
    # StartServers: number of server processes to start
    # MinSpareServers: minimum number of server processes which are kept spare
    # MaxSpareServers: maximum number of server processes which are kept spare
    # MaxClients: maximum number of server processes allowed to start
    # MaxRequestsPerChild: maximum number of requests a server process serves
    <IfModule mpm_prefork_module>
        StartServers          5
        MinSpareServers       5
        MaxSpareServers      10
        MaxClients          150
        MaxRequestsPerChild   0
    </IfModule>
    # worker MPM
    # StartServers: initial number of server processes to start
    # MaxClients: maximum number of simultaneous client connections
    # MinSpareThreads: minimum number of worker threads which are kept spare
    # MaxSpareThreads: maximum number of worker threads which are kept spare
    # ThreadsPerChild: constant number of worker threads in each server process
    # MaxRequestsPerChild: maximum number of requests a server process serves
    <IfModule mpm_worker_module>
        StartServers          2
        MinSpareThreads      25
        MaxSpareThreads      75 
        ThreadLimit          64
        ThreadsPerChild      25
        MaxClients          150
        MaxRequestsPerChild   0
    </IfModule>
    # event MPM
    # StartServers: initial number of server processes to start
    # MaxClients: maximum number of simultaneous client connections
    # MinSpareThreads: minimum number of worker threads which are kept spare
    # MaxSpareThreads: maximum number of worker threads which are kept spare
    # ThreadsPerChild: constant number of worker threads in each server process
    # MaxRequestsPerChild: maximum number of requests a server process serves
    <IfModule mpm_event_module>
        StartServers          2
        MaxClients          150
        MinSpareThreads      25
        MaxSpareThreads      75 
        ThreadLimit          64
        ThreadsPerChild      25
        MaxRequestsPerChild   0
    </IfModule>
    # These need to be set in /etc/apache2/envvars
    User ${APACHE_RUN_USER}
    Group ${APACHE_RUN_GROUP}
    #
    # AccessFileName: The name of the file to look for in each directory
    # for additional configuration directives.  See also the AllowOverride
    # directive.
    #
    AccessFileName .htaccess
    #
    # The following lines prevent .htaccess and .htpasswd files from being 
    # viewed by Web clients. 
    #
    <Files ~ "^\.ht">
        Order allow,deny
        Deny from all
    </Files>
    #
    # DefaultType is the default MIME type the server will use for a document
    # if it cannot otherwise determine one, such as from filename extensions.
    # If your server contains mostly text or HTML documents, "text/plain" is
    # a good value.  If most of your content is binary, such as applications
    # or images, you may want to use "application/octet-stream" instead to
    # keep browsers from trying to display binary files as though they are
    # text.
    #
    DefaultType text/plain
    #
    # HostnameLookups: Log the names of clients or just their IP addresses
    # e.g., www.apache.org (on) or 204.62.129.132 (off).
    # The default is off because it'd be overall better for the net if people
    # had to knowingly turn this feature on, since enabling it means that
    # each client request will result in AT LEAST one lookup request to the
    # nameserver.
    #
    HostnameLookups Off
    # ErrorLog: The location of the error log file.
    # If you do not specify an ErrorLog directive within a <VirtualHost>
    # container, error messages relating to that virtual host will be
    # logged here.  If you *do* define an error logfile for a <VirtualHost>
    # container, that host's errors will be logged there and not here.
    #
    ErrorLog /var/log/apache2/error.log
    #
    # LogLevel: Control the number of messages logged to the error_log.
    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    #
    LogLevel warn
    # Include module configuration:
    Include /etc/apache2/mods-enabled/*.load
    Include /etc/apache2/mods-enabled/*.conf
    # Include all the user configurations:
    Include /etc/apache2/httpd.conf
    # Include ports listing
    Include /etc/apache2/ports.conf
    #
    # The following directives define some format nicknames for use with
    # a CustomLog directive (see below).
    # If you are behind a reverse proxy, you might want to change %h into %{X-Forwarded-For}i
    #
    LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
    LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %O" common
    LogFormat "%{Referer}i -> %U" referer
    LogFormat "%{User-agent}i" agent
    #
    # Define an access log for VirtualHosts that don't define their own logfile
    CustomLog /var/log/apache2/other_vhosts_access.log vhost_combined
    # Include of directories ignores editors' and dpkg's backup files,
    # see README.Debian for details.
    # Include generic snippets of statements
    Include /etc/apache2/conf.d/
    # Include the virtual host configurations:
    Include /etc/apache2/sites-enabled/
    

    Heres my httpd.conf file:

    RewriteMap makelc int:tolower
    <Directory "/var/www/html">
    Options FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
    RewriteEngine on
    RewriteRule ^(.*) ${makelc:$1} [NC,L]
    </Directory>
    
     
  • James Butler

    James Butler - 2010-01-15

    Right. Apache2.

    The configuration file(s) you need are in your /etc/apache2/sites-enabled directory, and would look something like "000-default", for the default domain.

    So, delete the stuff you put in httpd.conf and modify the existing Directory or VirtualHost entry in the appropriate /etc/apache2/sites-enabled file for the domain you want this to apply to. You should also see the AllowOverride directive in that same location, and you can enable .htaccess for that Directory/VirtualHost by either removing the AllowOverride line completely or by changing its value from 'None' to 'All'.

    I'm going offline for the day. Hope everything goes smoothly for you, tonight! I'll check back in the morning.

     
  • nate faulds

    nate faulds - 2010-01-15

    alright, so edit the virtual host config, like this file: /etc/apache2/sites-available/default

     
  • James Butler

    James Butler - 2010-01-15

    Correct.

    Your goal is to modify the default settings of the domain you want to affect. So go to wherever those settings are and add your rewrite stuff to those instructions … wherever they are.

     
  • nate faulds

    nate faulds - 2010-01-16

    IT WORKED, thankyou sooooooooooooo much

     
  • nate faulds

    nate faulds - 2010-01-16

    is it ok if i PM you about probs like this one?

     

Log in to post a comment.

MongoDB Logo MongoDB