Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I read SO topics on the matter. However, they only apply to single hosts.

I would like to have a uniform rule for all the server for current and future domains.

Basically, I want to remove www from all requets and leave only domain name to be accessible.

Apache also serves HTTPS, so how to apply this to all?

Thanks!

http://www.example.com >> http://example.com
https://www.example.com >> https://example.com

Maria

share|improve this question

migrated from stackoverflow.com Sep 12 '10 at 7:16

This question came from our site for professional and enthusiast programmers.

    
There is exactly 2074 questions with mod_rewrite tag. What exactly is being asked in those questions that is not asked in mine? –  user54006 Sep 11 '10 at 21:12
1  
Who knows? Perhaps they escaped my attention ;) All I can tell you is that you question would be better asked on serverfault.com as per the FAQ. –  spender Sep 11 '10 at 21:15
    
@MarialKeys - Are you looking for a solution that avoids touching the <VirtualHost>s at all, or? –  Tim Stone Sep 11 '10 at 21:15
    
@spender - General mod_rewrite questions are appropriate for Stack Overflow. Depending on exactly what's being asked, this might be considered more of an Apache question, but it doesn't stray so far that I'm inclined to vote to move, personally. :) –  Tim Stone Sep 11 '10 at 21:17
    
Guys, this is a mod_rewrite question. But SO folks have always been helpful to me, so even though I feel insulted, I won't argue with you. If you like, I will move this to serverfault. –  user54006 Sep 11 '10 at 21:20

1 Answer 1

What you're describing is (seemingly) impossible using mod_rewrite, to a particular degree.

Configurations from multiple "levels" do not stack in mod_rewrite, so a rule set defined in the main server configuration will not be automatically imported and applied to a request that is delegated to a <VirtualHost>.

However, assuming that you have to assign the <VirtualHost> at some point anyway, if you don't plan to have any other rules within the <VirtualHost> body (if they're in .htaccess or a <Directory> section, that would be fine), you could play around with the following.

In the main configuration, we'll take my answer to this related question and apply it:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.
RewriteCond %{HTTPS}s ^on(s)|off
RewriteCond http%1://%{HTTP_HOST} ^(https?://)(www\.)?(.+)$
RewriteRule ^ %1%3%{REQUEST_URI} [R=301,L]

Then, in each of our <VirtualHost> sections, we can "import" it using the RewriteOptions directive:

RewriteEngine On
RewriteOptions inherit

Alternatively, of course, you could simply copy the rule set to each <VirtualHost> in its entirety, but this keeps the relevant rule set in a single location.

Note that the reason I suggest not having additional rules in the <VirtualHost> directly in this case is that the inherited rules are included after whatever you've defined in the current level of the configuration, which could have unintended side-effects.

share|improve this answer
    
Thanks Tim, you are a lifesaver. Works for me! It also works for the vhosts without RewriteOptions or RewriteEngine. Can you care to elaborate what exactly we are doing here? –  user54006 Sep 11 '10 at 21:50
    
I have a domain at #1 spot in Google with www. I think I will lose my position... but I am sick and tired of this www issue and want to get over with it once and for all. –  user54006 Sep 11 '10 at 21:53
    
@MarialKeys - Hmm, well, I had originally thought it should work automatically if you didn't have anything related to mod_rewrite in the virtual host, but I tried it on my local test server and it wasn't importing the main configuration's rule set without the explicit inherit. If it works for you without it, good stuff. Otherwise, what I had put will force it to pull in the rules from the main configuration. I need to review the source code again to see exactly what goes on there, since my answer may be a bit misleading about what you can and can't expect to happen automatically. –  Tim Stone Sep 11 '10 at 21:56
    
@Tim, I have rewrite rules in: <directory /var/www/html>. So it probably affects all without explicit inherit option. Can this be? –  user54006 Sep 11 '10 at 22:03
    
@MariaKeys - Ah, I see, that could be. I'm not 100% sure how you have your various hosts setup as far as directory roots, etc., so that does come into play. Consider what I've described here in that case, which is similar to this scenario (<Directory> is equivalent to .htaccess in this scenario)...and hopefully I can get a more clear description before I say anything overly incorrect about it. –  Tim Stone Sep 11 '10 at 22:14

Your Answer

 
discard

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