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.

Configuring .htaccess

June 20, 2011 8:45am

Subscribe [2]
  • #1 / Jun 20, 2011 8:45am

    kscot

    91 posts

    I’m looking for some help in writing an .htaccess file. In addition to removing “index.php” from the URL on the front-end, for SEO purposes I’d also like to:

    1. Remove “www” so all pages are served as “http://mydomain.com”
    2. Remove trailing slashes

    The following code seems to work OK for me:

    RewriteEngine on
    
    RewriteBase /
    
    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} ^(.+)/$
    RewriteRule ^(.*)/$ /$1 [L,R=301]
    RewriteCond $1 !^(images|system|favicon\.ico|robots\.txt|sitemap\.xml|index\.php|index\.htm) [NC]
    RewriteRule ^(.*)$ /index.php/$1 [L]

    However, I can’t access the control panel without including “index.php” in the URL, and links to thumbnail images in the control panel aren’t working. If anyone has a better, more efficient way of writing the .htaccess file to accomplish the above-stated goals I’d appreciate any suggestions.

  • #2 / Jun 20, 2011 11:45am

    Kevin Thompson’s Blog has an excellent post on this topic.  We use it, along with a few other tweaks, on all of our EE sites.

    # Redirect index.php Requests
    RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
    RewriteCond %{THE_REQUEST} !/your-ee-system-dir-goes-here/.*
    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]
  • #3 / Jun 20, 2011 4:58pm

    Sue Crocker

    26054 posts

    Thanks for the assist, Nick!

    kscot - will that .htaccess work for you?

  • #4 / Jun 22, 2011 6:24pm

    kscot

    91 posts

    Nick,

    That wasn’t exactly what I was looking for but a good head’s up nonetheless. Had a gander at the post you referred to and have added that bit to my default .htaccess file.

    My original problem was I kept getting an error when I would attempt to navigate to the control panel when I tried to strip the trailing slash from the URL. I noticed that I don’t have any trouble when I instead always add trailing slashes. So that’s what I’ve decided to do. Here’s what my default .htaccess for EE installs now looks like:

    <IfModule mod_rewrite.c>
    
    # Enable Rewrite Engine
    # ------------------------------
    RewriteEngine On
    RewriteBase /
    
    # Add trailing slash to all urls
    # ------------------------------
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} ^(.+)/$
    RewriteRule ^(.+)/$  /$1 [R=301,L]  
    
    # Remove www from url
    # ------------------------------
    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>

    My main motivation is for removing “www” and always adding the trailing slash is SEO.

  • #5 / Jun 23, 2011 2:52pm

    Sue Crocker

    26054 posts

    Glad that .htaccess works for you. If anything else comes up, please do let us know in a new thread..

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases