Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Reading up on doing url rewrites in Apache2, I found this:

"In general, you should never use .htaccess files unless you don't have access to the main server configuration file.

Okay, great. But there is no information anywhere about how to do it in the server configuration file. So before I mess stuff up, can I safely use the same rewrite directives, like

<IfModule mod_rewrite.c>    
    Options +FollowSymLinks
    RewriteEngine On
    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteCond %{SCRIPT_FILENAME} !-f
    RewriteRule ^test/(\w+)$ test.php?n=$1 [L]
</IfModule>

in /etc/apache2/apache2.conf? /etc/apache2/httpd.conf is blank, but I suppose I could do it there too?

Another question, should the rewrite rule paths be prefixed with /var/www/ or can I do it relative to the site root? Thanks.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

I don't necessarily agree with their reasons for claiming you should avoid .htaccess files. Their reasons are:

  • Reason 1 is performance - Apache needs to scan directories and read and parse the .htaccess files at run time. This isn't a huge burden and caches well.

  • Reason 2 is security. You're allowing users to modify Apache's configuration. But they're only modifying its local configuration for that particular directory, and it's possible to specify in Apache's global configuration which directives they are and aren't allowed to modify. Plus, if users having access to edit .htaccess files is a security problem for you, then surely also allowing the same users to use .php files is an even bigger security problem.

There's still some benefits to using .htaccess. It's somewhat of a de facto standard now so much so that you can pretty much depend on being able to use it on shared hosting, and many web application software packages make use of them in their own software. And it means that local Apache configuration is kept with the application it's configuring which might be good for backups, version control etc.

Anyway, whether you want to heed this advice is up to you.

If you do want to move to using Apache's global configuration, you can do as in your example.

However, the global apache2.conf file can be somewhat of a mess, and it was installed by the Apache2 package itself. For a more portable solution, put your rules into individual config files:

  • Apache2 reads all config files in /etc/apache2/conf.d/. This can be good for any general configuration directives you want to add that don't relate to any particular virtual host. Just add a new file there and put config into it.

  • Apache2 stores config files for each virtual host in /etc/apache2/sites-available/. That is a suggested place for putting in configuration for each virtual host, in separate files. Then, enable and disable these virtual hosts with a2ensite and a2dissite.

    Assuming that these configuration directives apply specifically apply to a certain virtual host I'd choose the latter - put it in where-ever you've put the configuration for that virtual host (which I'd recommend placing in /etc/apache2/sites-available/.

share|improve this answer
    
Thanks! So assuming I am using the default site, what would the file name and path be? Guessing the path might be /etc/apache2/sites-available/default/ but unsure about file name –  inorganik Nov 28 '12 at 5:34
    
You just put files directly into /etc/apache2/sites-available/ rather than creating subdirectories. There should already be some in there including the "default" which should be a file. If you want to edit the default site (ie you're not doing any virtual hosts), by all means edit the existing file (or copy it to a new file and disable the existing one if you want it to be portable). –  neon_overload Nov 28 '12 at 6:38
    
Much thanks. Got it working now. Added the rules in /etc/apache2/sites-available/default/ under <Directory /var/www/> –  inorganik Nov 28 '12 at 20:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.