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.

Issue with index.php and forcing www

July 17, 2012 4:48pm

Subscribe [1]
  • #1 / Jul 17, 2012 4:48pm

    Danny Veiga

    58 posts

    I never had this issue come up until a client told me about it. For example, if you have a site that forces www via htaccess, and you have index.php being removed - when you go to a particular page (domain.com/about) via non-www method it’ll throw index.php in the loop.

    Here’s what my htaccess looks like:

    RewriteEngine On
    RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php?/$1 [L]
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^(.*)$ <a href="http://www.%{http_host}/$1">http://www.%{http_host}/$1</a> [R=301,L]
    
    ## EXPIRES CACHING ##
    <IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpg "A2592000"
    ExpiresByType image/jpeg "A2592000"
    ExpiresByType image/gif "A2592000"
    ExpiresByType image/png "A2592000"
    ExpiresByType text/css "access 1 month"
    ExpiresByType application/pdf "access 1 month"
    ExpiresByType text/x-javascript "access 1 month"
    ExpiresByType text/javascript "access 1 month"
    ExpiresByType application/x-shockwave-flash "access 1 month"
    ExpiresByType image/x-icon "A2592000"
    ExpiresDefault "access 2 months"
    </IfModule>
    ## EXPIRES CACHING ##
    
    # gzip compression.
    <IfModule mod_deflate.c>
    
    # html, txt, css, js, json, xml, htc:
      AddOutputFilterByType DEFLATE text/html text/plain text/css application/json
      AddOutputFilterByType DEFLATE text/javascript application/javascript application/x-javascript
      AddOutputFilterByType DEFLATE text/xml application/xml text/x-component
    
    # webfonts and svg:
      <FilesMatch "\.(ttf|otf|eot|svg)$" >
        SetOutputFilter DEFLATE
      </FilesMatch>
    </IfModule>

    So again, when someone goes to http://www.domain.com/about everything works fine, but if they type domain.com/about it’ll load as domain.com/index.php?/about—- can’t figure how to remove index.php from the non forced www.

  • #2 / Jul 17, 2012 5:07pm

    What happens if you have the force www rule first, as follows:

    RewriteEngine On
    
    #Force WWW
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^(.*)$ <a href="http://www.%{http_host}/$1">http://www.%{http_host}/$1</a> [R=301,L]
    
    #Process with index.php
    RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php?/$1 [L]
  • #3 / Jul 17, 2012 5:10pm

    It just occurred to me that the problem might be that you don’t actually have a rule that will remove index.php from the request… you probably need something like this….

    #Force WWW
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^(.*)$ <a href="http://www.%{http_host}/$1">http://www.%{http_host}/$1</a> [R=301,L]
    
    # ---------------------------------------------------------------------
    # Remove index.php from the URL
    #
    # In any request contains "index.php"  ...
    # ... and is an Apache GET request ....
    # ... and isn't in the EE system directory ... (assuming you still have a system folder that contains index.php)
    # Then ...
    # ... redirect to the requested location without "index.php"
    # ---------------------------------------------------------------------
    
    RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC] 
    RewriteCond %{THE_REQUEST} !/system/.*
    RewriteRule (.*?)index\.php/*(.*) $1$2 [R=301,L]
    
    #Process with index.php
    RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php?/$1 [L]
  • #4 / Jul 17, 2012 7:37pm

    Danny Veiga

    58 posts

    Awesome, thanks for that last bit of htaccess code Nick - worked perfectly!

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

ExpressionEngine News!

#eecms, #events, #releases