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

I am having trouble doing what i want to do with the following setup.

I would like to remove all WWW, and also forward index.html to root dir. I would like this to be for all domains, so i am doing inside httpd.conf directory directive.

I tried many variations with no success. Latest version is below (domains are inside /var/www/html, in seperate directories).

http://www.example.com/index.html > http://example.com 
http://www.example.com/someother/index.html > http://example.com/someother/

Thanks,

Maria

<Directory "/var/www/html/*/"> 
    RewriteEngine on 
    RewriteBase /  

    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]  
    RewriteRule ^(.*)$ http://%1/$1 [R=301,L]   

    #RewriteCond %{REQUEST_URI} /^index\.html/        
    RewriteRule ^(.*)index\.html$ / [R=301,L] 


        Options ExecCGI Includes FollowSymLinks 
        AllowOverride AuthConfig 
        AllowOverride All                       
        Order allow,deny 
        Allow from all 

 </Directory>    
share|improve this question
    
have you tried RewriteRule ^index\.html$ / [R=301,L] no need for RewriteCond, Your first rule is ok since it will take care of removing the www and redirecting the domain so when it redirects it will hit rule number 2 –  Prix Oct 17 '10 at 16:01
    
Hello Prix. Yes I did try that. That one is wrong to start with, because it will not redirect my 2nd example above. It will redirect all index.htmls. But anyway, even that rule is not working inside directory entry properly... making it: example.com//var/www/html/example –  user54006 Oct 17 '10 at 22:58
    
Oh hehe my bad i didnt noticed that you were trying to use it on Directory, that's right it does take internal path into consideration etc. –  Prix Oct 18 '10 at 4:36

2 Answers 2

I have not tried this, but I would approach the www redirect by creating two vhosts. One for www and one for non-www, with the only directive in the www one to redirect to the other

<VirtualHost *:80>
    ServerName www.example.com
    Redirect / http://example.com
</VirtualHost>

<VirtualHost *:80>
    ServerName example.com
    DocumentRoot /path/to/docroot
    ... other stuff as needed ...
</VirtualHost>

If your index.html actually has your content in it, you need a DirectoryIndex that specifies index.html is a valid index in your httpd.conf:

DirectoryIndex index.html

This is typically the default, so perhaps more information is needed on what you want to accomplish with that.

share|improve this answer
    
Hi Alex, what i wanted to accomplish is clearly explained above. I need to get mod_rewrite working in directory level. www > non-www working fine above. index.html is not. I have DirectoryIndex also. –  user54006 Oct 17 '10 at 14:51
    
aaah...I missed the key "for all domains" line there :) –  Alex Oct 17 '10 at 15:11
    
Apparently, there are a lot of mistakes with my usage. For example, L meaning LAST stops processing. So 2nd rule is not seen. –  user54006 Oct 17 '10 at 15:59

This solves it for me. As I suspected, there is a whole lotta difference where rewriterule is applied. Many people including me seems to be unaware of this.

http://wiki.apache.org/httpd/RewriteContext

The Apache HTTPD Server deals with requests in discrete phases. While this is usually transparent to the user and administrator it does have an effect on the behaviour of mod_rewrite when rulesets are placed in different contexts. To oversimplify a little, when rules are placed in VirtualHost blocks (or in the main server context) they get evaluated before the server has yet mapped the requested URI to a filesystem path. Conversely, when rules are placed in .htaccess files, or in Directory blocks in the main server config, they are evaluated after this phase has occured.

share|improve this answer

Your Answer

 
discard

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