Search code examples
apachecodeignitermod-rewriteurl-rewritingnormalization

URL Canonicalization - Apache mod_rewrite


I am probably attempting to take URL Canonicalization a bit too far but here it goes anyway.

Basically I am looking to the following:

  1. 301 redirect every base url to http://www.mydomain.com/ some pages are https it should recognize that and continue to use https where already used/requested
  2. 301 redirect away any trailing slashes ie http://www.mydomain.com/page/ becomes http://www.mydomain.com/page (I already have a line of code that finds the index.php page - this site is built on Codeigniter)
    • I Don't want the base url to have the slash stripped that is the only time a slash should be left behind
  3. Find any instances of index.php (in the front middle or end of the url) and 301 redirect them out
  4. 301 redirect any use of my ip address to the actual domain

Here is what I have so far in my htaccess file in my root directory:

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]

I am stuck right now any help would be greatly appreciated!!

Revisions!!

I have made some progress and here is what I have so far

<IfModule mod_rewrite.c>
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]
</IfModule>

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!


Solution

  • RewriteEngine on
    
    # index.php remove any index.php parts
    RewriteCond %{THE_REQUEST} /index\.(php|html)
    RewriteRule (.*)index\.(php|html)(.*)$ $1$3 [R=301,L]
    
    # force www. (also does the IP thing)
    RewriteCond %{HTTP_HOST} !^www\.paolienvelope\.com [NC]
    RewriteRule ^(.*)$ http://www.paolienvelope.com/$1 [R=301,L]
    
    # remove tailing slash
    DirectorySlash off
    RewriteCond $1 !^(index\.php|images|assets|downloads|css|js)
    RewriteRule ^(.*)/$ $1 [R=301,L]
    
    # codeigniter direct
    RewriteCond $1 !^(index\.php|images|assets|downloads|css|js|robots\.txt)
    RewriteRule ^(.*)$ /index.php/$1 [L]