CodeIgniter Forums

Full Version: .htaccess to remove /index/ action from URL
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2

El Forum

[eluser]Ben Edmunds[/eluser]
Hey everybody,

I can't figure out how to accomplish this and google is no help. What I need to do is remove the /index/ from my URLs. So if I go to http://www.mysite.com/welcome/index/ I need it to rewrite to http://www.mysite.com/welcome/


Thanks in advance,

El Forum

[eluser]Flemming[/eluser]
Hi Ben,

Your question isn't quite clear to me ... do you mean that if someone was to deliberately type /index at the end of your url, it should be removed?

El Forum

[eluser]Ben Edmunds[/eluser]
Yea basically. You know in codeigniter you can use the url mysite.com/controller/action. if the URL is mysite.com/controller/index I want to rewrite it to mysite.com/controller.

It will still go to the same place but the SEO guys here want it setup without the index.


Thanks,

El Forum

[eluser]n0xie[/eluser]
It automatically goes to the index if there is no action specified...

El Forum

[eluser]Flemming[/eluser]
Ahh OK I think I'm with you! First you need to look in application > config > config.php and edit the line :

Code:
$config['index_page'] = "index.php";
remove 'index.php'

Then you will need to use something like this in your .htaccess

Code:
RewriteCond $1 !^(index\.php|index\.html|assets.*|robots\.txt)
RewriteRule ^(.*)$ /index.php?/$1 [L]

depending on your server you may or may not need the ? after index.php

I hope that is what you were looking for?

Edit: and of course n0xie is right - it automatically goes to the index method if no method is supplied in your url!

El Forum

[eluser]Ben Edmunds[/eluser]
No thats not what I'm looking for. I'm not explaining it correctly...

n0xie is right that CI automatically routes to the index method if no method is supplied in the url; basically what I want to do is if the method = index then remove the method so the url only contains the controller (class) and CI will automatically route to the index method.

Make sense yet?


Thanks,

El Forum

El Forum

[eluser]Ben Edmunds[/eluser]
That will work. I can just do a 301 if the method = index.



Thanks for the help!

El Forum

[eluser]Flemming[/eluser]
Am glad you got there in the end but I still don't fully understand why you needed to do that?

If you have a controller named 'welcome' with 2 methods, say 'index()' and 'hello()' ... the url for the default (index) method would be yoursite.com/welcome ... and for the hello method would be yoursite.com/welcome/hello

under what circumstances would you ever have to expose the url yoursite.com/welcome/index ?

am still puzzled! Tongue

El Forum

[eluser]n0xie[/eluser]
[quote author="Ben Edmunds" date="1254257902"]No thats not what I'm looking for. I'm not explaining it correctly...

n0xie is right that CI automatically routes to the index method if no method is supplied in the url; basically what I want to do is if the method = index then remove the method so the url only contains the controller (class) and CI will automatically route to the index method.

Make sense yet?


Thanks,[/quote]
No not really. You want to filter the /index if it is given, but why would you do that. There only way an user would ever go to /controller/index is if YOU directed him to there. If you would direct your user to /controller the whole issue wouldn't be... an issue.

So I really don't understand why you would explicitly route someone to somewhere when the chance that someone would accidentally get there in the first place is close to zero.

Especially if there is no other reason than SEO... If the SEO guy is REALLY that worried you could just as easy use canonical tags so spiders would know that it's not duplicate content but the same resource with 2 url's.
Pages: 1 2