ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Using htaccess to elegantly remove index.php and www

December 07, 2011 12:57pm

Subscribe [2]
  • #1 / Dec 07, 2011 12:57pm

    John St-Amand

    865 posts

    I don’t know how to write htaccess rules so I’m trying to piece together an htaccess file that would elegantly do two things:

    1) rewrite all http://www.domain.com urls to domain.com
    2) remove index.php while ensuring that index.php versions of the same URLs do not render separately and instead redirect/rewrite to their non-index.php equivalent
    3) in a perfect word, condition 1 would work for http and https

    So what I’ve pieced together so far, hopefully correctly, is this:

    <IfModule mod_rewrite.c>
    
    # Enable Rewrite Engine
    # ------------------------------
    RewriteEngine On
    RewriteBase /
    
    # Redirect index.php Requests
    # ------------------------------
    RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
    RewriteCond %{THE_REQUEST} !/system/.*
    RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,L]
    
    # Redirect www Requests
    # ------------------------------
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
    
    # Standard ExpressionEngine Rewrite
    # ------------------------------
    RewriteCond $1 !\.(css|js|gif|jpe?g|png) [NC]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php/$1 [L]
    
    </IfModule>

    Can any htaccess gurus out there tell me if this is correct?

    Thanks!
    John

  • #2 / Dec 10, 2011 5:10pm

    Michael Rog

    179 posts

    The EE User Guide has instructions for removing index.php from URLs:

    http://ellislab.com/expressionengine/user-guide/general/remove_index.php.html

    You look to be following this method, so I’d say you’re on the right track.

  • #3 / Dec 14, 2011 4:47pm

    John St-Amand

    865 posts

    So maybe like this?

    <IfModule mod_rewrite.c>
    
    # Enable Rewrite Engine
    # ------------------------------
    RewriteEngine On
    RewriteBase /
    
    # Redirect www Requests
    # ------------------------------
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
    
    # Redirect index.php Requests
    # ------------------------------
    RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
    RewriteCond %{THE_REQUEST} !/system/.*
    RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,L]
    
    # Standard ExpressionEngine Rewrite
    # ------------------------------
    RewriteCond $1 !\.(css|js|gif|jpe?g|png) [NC]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php/$1 [L]
    
    </IfModule>
.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases