I have just completed this tutorial (and afew others) on how to use mod_rewrite with apache: http://www.workingwith.me.uk/articles/scripting/mod_rewrite . However, they all focus on using the .htaccess file to rewrite URLs

How can you use this rewrite engine module from the apache.conf / httpd.conf files or just from within the '/etc/apache2/sites-enabled' directory itself... ? Examples of any additional syntax needed on how to do this would be useful.

I have root access to my system so I would prefer to avoid using .htaccess files if at all possible. My system is Ubuntu-Server 10.04


Syntax is the same, but you have to make sure that you put your rules in the correct location within the httpd.conf file (or vhosts.conf or whatever is appropriate for your distro) depending on your requirements.

So if your rewrite rule is specific to a particular Virtual Host, then the rewrite rule would go in the container for that Virtual Host. If the rule is global (e.g. affects all sites), then you can put it anywhere in the httpd.conf file.

Documentation on mod_rewrite and context should be useful references for this. An Apache book is worth investing in if you will be doing a lot of this!

Out of interest, why do you want to avoid using .htaccess files for this? Changes in the .htaccess files are immediate, whereas changes in httpd.conf etc require apache restart to be effective.

  • 1
    One reason to remove .htaccess files is to speed up the server a bit - if you turn them off in the apache config then each page view doesn't require the server to go look for a .htaccess file.
    – Bart B
    Oct 13 '10 at 13:50
  • -1 meta.stackexchange.com/questions/22232/…
    – Prix
    Oct 13 '10 at 13:50
  • yes I have seen books just dedicated to apache mod_write, I will start to look over those links for answers thanks. its for a personal website so im not too concerned about restarts, im also curious if it is possible... .htaccess files effect performance of the server as apache has to check each directory on the request if they exist and apply their rules before displaying a page, VS using the *.conf files that are pre-loaded rules on restart.
    – JB87
    Oct 13 '10 at 14:00
  • 2
    @Prix - where is the expletive?
    – dunxd
    Oct 13 '10 at 15:08
  • 1
    yes exactly, I just wasnt sure how to move the rules up to config files before this thread. also, it would be difficult to test this way without re-enabling 'AllowOverride' in the site config (requiring a restart now anyway). as for performance hit im not sure how you could test this, but I have found an article which describes the problems with .htaccess in greater detail if it is of use to you karakas-online.de/EN-Book/htaccess-file.html
    – JB87
    Oct 13 '10 at 15:49

I know it's a snotty answer - but it really is the right one - RTFM! (to any prudes out there, the F stand for 'fine', if you read something else it's because YOU have a potty-mouth, not me)

The Apache documentation clearly says where (i.e. in what contexts) directives can be used. If in doubt, it's always there in black and white for every single directive you can use. If you're going to be admining Apache, you simply cannot avoid learning to read Apache documentation.

In this case, it says that you can use RewriteRule in "server config, virtual host, directory, .htaccess". (http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriterule)

To answer your other question, no, the syntax does not change. In reality the chances are you are using named virtual hosts, so the logical place for your rewrite rules is inside the relevant VirtualHost directive.


  • thanks for the clear answer Bart. I will be doing alot of apache admin work, is there any publishers you would recommend? I have managed to get this working by adding the necessary rules to '/etc/apache2/sites-enabled/mysite' with the website directory tags.. is this the most logical way to go about it? or would I be better off pushing to a different conf file, or is it possible to include custom conf files within the directory tag?
    – JB87
    Oct 13 '10 at 14:36
  • Watch out - Prix will be by in a minute to vote down your helpful answer...
    – dunxd
    Oct 13 '10 at 15:12
  • Is it downvote for no apparent reason today or something? Bart's answer is good, answering the question and providing useful links. I don't see why it deserves any downvotes.
    – dunxd
    Oct 14 '10 at 8:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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