• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Redirect index function

Hey guys,

Im trying to decide what will be the best way to disallow access to the index function. Basically I think it just looks rubbish to have e.g. messages/index. At the moment I, e.g. redirect to messages/inbox but its a bit of a pain having to put stuff in the index function in the messages controller to redirect it.

I was wondering if someone would know a better way of doing it?

Search the forum's for .htaccess


Hmmm, I dont mean i want to rewrite the "index.php" bit out of the URL, I mean i want to disable the index function in each of the controllers. Basically, my databases is setup to retriev page information but i dont want duplicates for things like domain.com/ and domain.com/index/ as I dont want index to be accessible. You understand?

I basically dont want to have to redirect in every single controller index function if someone goes to domain.com/index.



the index is the "default" method in any controller, but you do not have to use it.

you can create any method you want and have that work as the default
still create the index method but have it call the other method
class Messages extends Controller {

  function Messages() {

  function index() {

  function inbox() {

Then if the url is http://myhost.com/messages, it will call the index method which will call the inbox. The url will be http://myhost.com/messages until they click on other links which can always go to http://myhost.com/messages/inbox

So the user never has to see http://myhost.com/messages/index

On the other hand the default method is called index for a reason.
The idea is that if you use the url suffix, such as .html then you can disguise all of the pages as HTML
http://myhost.com/messages/index.html would be the default page, which would be what the average web user would expect to be the default url.

Thanks for the replies! I dont think you quite understand what I want. Im not using, e.g. index.html at the end. If someone ever requests /page/index I want it to always redirect to /page. That way index is never in the url. Im thinking the best way is with htaccess but I cant figure it out as Im not great with the redirections. Could someone perhaps take a look and tell me why this doesnt work? So basically, I need to capture any request uri that ends in /index and redirect so that it doesnt end in /index

these are the different options ive been trying

RewriteRule ^(.*)\/index$ index.php?/$1 [R=301,L]

#RewriteCond %{THE_REQUEST} ^([/a-zA-Z0-9_-]){0,9}\/index$
#RewriteRule ^(.*)index\.php$ /$1 [R=301,L]

RewriteCond %{THE_REQUEST} ^.*/index
RewriteRule ^(.*)index$ index.php?/$1 [R=301,L]


ok s now ive got it working, but its appending a '/' on the end of the url. i.e. .com/support/index redirects to .com/support/ is there anyway to remove the end '/'?

Im using:

RewriteRule ^(.*)index$ $1 [R=301,L]

thanks everyone!

Its ok ive done it now. i just put the '/' in front of index so that it matches with that instead of the .* bracket.! cheers guys :-)

[eluser]Mad Maurice[/eluser]
why not using the url_helper?

function index() {

Because 1) I would have to do that in every controller, 2) I generally want an index function in each controller :-)

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.