Welcome Guest, Not a member yet? Register   Sign In
Little tutorial for creating pages with .html extension in CodeIgniter
#1

[eluser]Zorancho[/eluser]
If you want to have urls with .html extension with CI, you can achieve it in two ways. I will try to explain both of them.
1. .htaccess file
This will rewrite everything in the first part of the url that's ending with .html and it doesn't contain / slash to pages controller and view action passing the link as parameter.
www.site.com/some-page.html will be redirected to pages/view/some-page
Code:
<IfModule mod_rewrite.c>
    RewriteEngine On
    
        RewriteRule ^([^\/]*).html$ pages/view/$1      
  
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule (.*) index.php/$1 [L]
</IfModule>

This will redirect www.site.com/product/some-product.html to products/view/some-product
If you print out $this->uri->segment_array() it gives:
Code:
array(
   1 => products
   2 => view
   3 => some-product
);
Code:
<IfModule mod_rewrite.c>
    RewriteEngine On
    
        RewriteRule ^product/([^\/]*).html$ products/view/$1      
  
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule (.*) index.php/$1 [L]
</IfModule>
Sometimes this solution might not work, cause it depends on the hosting, then you have fallback to the CI's routing.
Open app application/config/routes.php.
You can add your own urls here.
This will redirect www.site.com/some-other-page.html to pages/view/some-other-page
same like you would add it in your htaccess file.
Code:
$route['^([^\/]*).html$'] = 'pages/view/$1';

You can even add multiple rules if you are using multi language application:
Code:
$route['^en/([^\/]*).html$'] = 'pages/view/$1'; //English
$route['^de/([^\/]*).html$'] = 'pages/view/$1'; //German
$route['^mk/([^\/]*).html$'] = 'pages/view/$1'; //Macedonian

The routing of CI is very practical and nice, it works almost same like htaccess file. Please share if someone has other solution Smile

There is also $config['url_suffix'] = ""; it's in the application/config/config.php file on line 37, but not sure how it serves the urls.
#2

[eluser]Eric Barnes[/eluser]
The $config[‘url_suffix’] = '.html'; would be the easiest method as CodeIgniter handles everything as normal but just adds the .html extension. I would try it first.




Theme © iAndrew 2016 - Forum software by © MyBB