title: URL Canonicalization - Apache mod_rewrite
body: <p>I am probably attempting to take URL Canonicalization a bit too far but here it goes anyway.  </p>
  
  <p>Basically I am looking to the following:</p>
  
  <ol>
  <li>301 redirect every base url to <a href="http://www.mydomain.com/" rel="nofollow">http://www.mydomain.com/</a> some pages are https it should recognize that and continue to use https where already used/requested</li>
  <li>301 redirect away any trailing slashes ie <a href="http://www.mydomain.com/page/" rel="nofollow">http://www.mydomain.com/page/</a> becomes <a href="http://www.mydomain.com/page" rel="nofollow">http://www.mydomain.com/page</a> (I already have a line of code that finds the index.php page - this site is built on Codeigniter)
  <ul>
  <li>I Don't want the base url to have the slash stripped that is the only time a slash should be left behind</li>
  </ul></li>
  <li>Find any instances of index.php (in the front middle or end of the url) and 301 redirect them out 
  <ul>
  <li>ie <a href="http://www.mydomain.com/index.php/page/index.php/" rel="nofollow">http://www.mydomain.com/index.php/page/index.php/</a> becomes <a href="http://www.mydomain.com/page" rel="nofollow">http://www.mydomain.com/page</a></li>
  </ul></li>
  <li>301 redirect any use of my ip address to the actual domain
  <ul>
  <li>ie 11.11.111.111/page/index.php would become <a href="http://www.mydomain.com/page" rel="nofollow">http://www.mydomain.com/page</a></li>
  </ul></li>
  </ol>
  
  <p>Here is what I have so far in my htaccess file in my root directory:</p>
  
  <pre><code>RewriteEngine on
  
  Options +FollowSymLinks 
  RewriteCond %{REQUEST_URI}   ^(.*)(/index\.php)$
  RewriteRule ^(.*)index\.php/$  http://www.mydomain.com/$1 [R=301,L]
  
  RewriteCond %{HTTP_HOST}   ^(mydomain\.com)(:80)? [NC]
  RewriteRule ^(.*)$  http://www.mydomain.com/$1 [R=301,L]
  
  RewriteCond %{HTTP_HOST}   ^11\.11\.111\.111$
  RewriteRule ^(.*)$  http://www.mydomain.com/$1 [R=301,L]
  
  #This makes sure that the server actually finds the index file although its not in the url
  RewriteCond $1   !^(index\.php|images|assets|downloads|css|js|robots\.txt)
  RewriteRule ^(.*)$  /index\.php/$1 [L]
  </code></pre>
  
  <p>I am stuck right now any help would be greatly appreciated!!</p>
  
  <h2>Revisions!!</h2>
  
  <p>I have made some progress and here is what I have so far</p>
  
  <pre><code>&lt;IfModule mod_rewrite.c&gt;
  RewriteEngine on
  
  # index.php to /
  RewriteCond %{THE_REQUEST} ^GET\ /.*/index\.(php|html)\ HTTP
  RewriteRule (.*)index\.(php|html)$ /$1 [r=301,L]
  
  # index.php to / at the base url
  RewriteCond %{THE_REQUEST} ^GET\ /index\.(php|html)\ HTTP
  RewriteRule (.*)index\.(php|html)$ /$1 [r=301,L]
  
  # force www.
  rewritecond %{HTTP_HOST} ^paolienvelope.com [nc]
  rewriterule ^(.*)$ http://www.paolienvelope.com/$1 [r=301,L]
  
  # force no IP
  RewriteCond %{HTTP_HOST} ^70.40.204.154
  RewriteRule ^(.*) http://www.paolienvelope.com/$1 [r=301,L]
  
  #codeigniter direct
  RewriteCond $1 !^(index\.php|images|assets|downloads|css|js|robots\.txt)
  RewriteRule ^(.*)$ /index.php/$1 [L]
  &lt;/IfModule&gt;
  </code></pre>
  
  <p>This successfully forces ip to url
  Removes index.php or index.html from the url but correctly directs to the index file despite
  makes sure the base url has www.
  Still dont have the code to remove the trailing slash from only the request
  any help would be appreciated!! Thanks!</p>