Welcome Guest, Not a member yet? Register   Sign In
Using static URL's
#1

[eluser]Sweepee[/eluser]
Hi all,

I'm trying to use static URL's in CodeIgniter, but can't find out how to do this!

To explain my problems, I'll give you some examples of what I want:

Code:
/index.html
goes to controller
Code:
home
function
Code:
index
Code:
/contact.html
goes to controller
Code:
contact
function
Code:
index
Code:
/links.html
goes to controller
Code:
links
function
Code:
index

I tried the following in
Code:
application/config/routes.php
:

Code:
$route["index.html"] = "home/index";
$route["contact.html"] = "contact/index";
$route["links.html"] = "links/index";

But this doesn't work! I always get the default controller when I surf to any of those URL's that I want. Can someone help me out here?

Thanks for any help!

Kind regards,
Sweepee
#2

[eluser]Isern Palaus[/eluser]
Take a look in your config.php, there is a variable to add a extension to all urls so if you add html it will be displayed .html.

Then your route will be $route['index'] = "home/index"; and will be accessible by index.html.
#3

[eluser]Sweepee[/eluser]
[quote author="Isern Palaus" date="1270010622"]Take a look in your config.php, there is a variable to add a extension to all urls so if you add html it will be displayed .html.

Then your route will be $route['index'] = "home/index"; and will be accessible by index.html.[/quote]

Thanks! I'll give it a try when I get back home!

Cheers
#4

[eluser]Sweepee[/eluser]
Hello again,

I'm still experiencing problems configuring the routes as I desire them.

The (relevant) configuration is as follows:

/system/application/config/config.php

Code:
$config['base_url']    = "http://localhost/ci/";
$config['index_page'] = "";
$config['url_suffix'] = "html";

/system/application/config/routes.php

Code:
$route['default_controller'] = "home";
$route['scaffolding_trigger'] = "";
$route['index'] = "home/index";
$route['contact'] = "contact/index";

/.htaccess

Code:
Allow From All
RewriteEngine On
RewriteRule !\.(js|ico|txt|gif|jpeg|jpe|jpg|png|css)$ index.php

All requests are redirected to index.php, except for JavaScript, CSS, text files and images.

When I surf to http://localhost/ci/contact.html for example, I always get the homepage (home/index).

Am I missing something here?

Thanks in advance for any help!!!

Kind regards,
Sweepee
#5

[eluser]cahva[/eluser]
Thats the expected behaviour because you are rewriting everything to plain index.php without segments(so index.php never gets your contact.html, links.html etc.).

Try a different .htaccess. Something like this:
Quote:RewriteEngine on
RewriteCond $1 !^(index\.php|assets|robots\.txt)
RewriteRule ^(.*)$ index.php?/$1 [L]

That is from one of the sites I've developed. I usually place all css, images etc. in the assets folder but that should give you a clue how to do proper rewrite.
#6

[eluser]Sweepee[/eluser]
Thanks!

I'll give it a try immediately!

Cheers!
#7

[eluser]Sweepee[/eluser]
My projects (with my needs) works with this HTACCESS file:

Code:
Allow From All
RewriteEngine On
RewriteCond $1 !^(index\.php|css|js|images|robots\.txt)
RewriteRule ^(.*)$ index.php?/$1 [L]

It's working now for these URL's:

/index goes to home/index
/contact goes to contact/index

But this doesn't work for /index.html and /contact.html...

How can I set this HTACCESS?

Don't I have to make a rule like:

"redirect every URL that ends on .html to index.php without that .html"

What do you think?

Also, that setting in config.php... I don't think it's helping my out, because of the comments in the config:

Code:
/*
|--------------------------------------------------------------------------
| URL suffix
|--------------------------------------------------------------------------
|
| This option allows you to add a suffix to all URLs generated by CodeIgniter.
| For more information please see the user guide:
|
| http://ellislab.com/codeigniter/user-guide/general/urls.html
*/

$config['url_suffix'] = "html";

It's only adding a suffix to all GENERATED URL's in CodeIgniter. Can be wrong though! Four happy days of CodeIgniter experience by now. Smile
#8

[eluser]cahva[/eluser]
It should be:
Code:
$config['url_suffix'] = ".html";
You forgot a dot there.
#9

[eluser]Sweepee[/eluser]
[quote author="cahva" date="1270096321"]It should be:
Code:
$config['url_suffix'] = ".html";
You forgot a dot there.[/quote]

Yup, noticed it before you replied.

But it didn't work when I added the dot.

This is what made it work completely (in /config/routes.php):

Code:
$route['default_controller'] = "home";
$route['scaffolding_trigger'] = "";
$route['index.html'] = "home/index";
$route['contact.html'] = "contact/index";
$route['disclaimer.html'] = "disclaimer/index";




Theme © iAndrew 2016 - Forum software by © MyBB