Welcome Guest, Not a member yet? Register   Sign In
Redirect 301 lowercase
#1

[eluser]ironlung[/eluser]
Hi all I meet my nemesis .htaccess once more. I want to redirect 301 all incoming urls with any capital to it's lowercase counterpart. On scouring the internet I found it was easy to do with something like this.

RewriteEngine On
RewriteMap lc int:tolower
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule (.*) ${lc:$1} [R=301,L]

Wrong! Reading further down every thread I find that RewriteMap MUST be in the httpd.conf which is a server level file. Of course I'm on shared hosting and there's not a hope in hell of getting that changed.

So can I do this in php easily? Do I have to hack index.php Sad I absolutely need the 301 redirect so I can't use the URI routing module or can I?

#2

[eluser]rana[/eluser]
You definitely can achieve 301 redirect from script hack. it even doesn't need to be in index.php(adding code there is a bad practice).

Use a base controller for your application, where all your controllers inherit that base controller. on the base controller's constructor, check the uri, if upercase, make it lower case and use "redirect({lowe_uri})" method.

Just to make sure, you are having a 301 redirect, before the redirect function, use "header ('HTTP/1.1 301 Moved Permanently');" method. You will be absolutely fine.

Hope this helps.
#3

[eluser]ironlung[/eluser]
[quote author="rana" date="1363770268"]You definitely can achieve 301 redirect from script hack. it even doesn't need to be in index.php(adding code there is a bad practice).

Use a base controller for your application, where all your controllers inherit that base controller. on the base controller's constructor, check the uri, if upercase, make it lower case and use "redirect({lowe_uri})" method.

Just to make sure, you are having a 301 redirect, before the redirect function, use "header ('HTTP/1.1 301 Moved Permanently');" method. You will be absolutely fine.

Hope this helps. [/quote]

Awesome, awesome, awesome that's good news thanks everything goes through either my data maintenance controller or another base controller so thats kewl. Yeah I do know it's bad practise to hack index.php that's why I put the little sad face after it Smile

Thank you so much for your solution.
#4

[eluser]boltsabre[/eluser]
No disrespect to Rana, most of the advice is spot on (ie, you can do 301 redirects in php, in your code, and it will do what you want it to), but you shouldn't do it in your base controller!

It will just clog up your base controller with useless code, and it's happening way too late in the process!

301 redirects, where possible, should be done AS SOON AS POSSIBLE. So, in CodeIgniter, you should create a hook, that fires pre-controller, and put your code in there. It's easy, the user documentation has a great walk through, you should be able to get your hook (but not the redirect code) set up in 10 minutes!

And why should you do it in a hook, and not in a base controller.

Well, from an ascetics point of view, this kind of redirect is a "server" level redirect (as you know, because you've tried doing it in your .htaccess), so it should be executed as soon as possible in your script to try to replicate this.

Also, by running it in an early hook, you're freeing up resources on your server. Basically, the hook is one of the first things to run, and it'll redirect the user to the correct page. But if you have it in your base controller then CI has to load a whole bunch of stuff such as loaders, autoloaded libraries, models, helpers, etc, config files, etc etc etc, all of which are using unnecessary server resources.

And lastly, it'll increase you page load speed for your users a little. Not much but a little. It may not be noticeable, but if you're paying this kind of attention to the little things right across the site then they all add up very quickly and can have quiet a big impact.

So in conclusion, do as Rana said, but do it in a hook!

Good luck.
#5

[eluser]ironlung[/eluser]
Ok I'll look into that now, thanks very much boltsabre...
#6

[eluser]TheFuzzy0ne[/eluser]
I don't see what the big deal is about editing your index.php file. It's the bootstrap for the entire system, and if you redirect there, then only one file is ever loaded (hooray!). You're expected to edit the file yourself, how else are you supposed to add new environments?

There's even a line in there:
Code:
// --------------------------------------------------------------------
// END OF USER CONFIGURABLE SETTINGS.  DO NOT EDIT BELOW THIS LINE
// --------------------------------------------------------------------

That in itself implies that you can pretty much do what you want to the file before that line, so long as you leave the bootstrap alone.

I've added all sorts of extra logic to mine, so that it can figure out which environment it's running in, and sets the ENVIRONMENT constant accordingly. I'm not saying this is the right way, but it's certainly the simplest way of doing it.

You should NEVER edit anything in the ./system directory, but I'd consider index.php as a sort of hybrid between ./system and ./application.

If it bothers you that much, you could always put the code into another file, and just include that from your index.php, but I don't see the point in loading an extra file for something so trivial, but it sure beats loading a hook.

Calling a hook will work just fine, but it's certainly not the most efficient way to do it.

PS. If you never hear from me again, it means I was wrong and have been smited for the error in my index.php ways.
#7

[eluser]ironlung[/eluser]
Ha ha ha thanks fuzzyone I've got it working in the hooks now. When no-ones looking I might slap it in the index.php lol.


I once saw some dandy put
Quote:Note to self, hack core at earliest convenience.
and whilst it made me chuckle, I've been scared of admitting of doing anything that may be seen to be not whiter than white ever since...


Anyway as usual thanks all...
#8

[eluser]TheFuzzy0ne[/eluser]
Hey, no problem! Glad you got it sorted.

I have absolutely nothing against you making a hook. I just wanted post my comments, since people seem to keep getting their panties in a twist at the thought of editing the index.php file.

CodeIgniter allows you to extend almost every aspect of the framework, so there shouldn't be any need to modify the core files, unless you simply don't understand CodeIgniter enough to do it properly, and are too stupid to find out. The only major exceptions are the database classes, but the only time I've ever wanted to change anything in there, has been to fix a bug.
#9

[eluser]ironlung[/eluser]
Ha ha ha ha...
#10

[eluser]boltsabre[/eluser]
Ah right, well yes, I was talking about hooks rather than in the base controller (which I assumed Rana was talking about "MY_Controller"), but perhaps s/he did mean the index.php file.

As does TheFuzzyOne, I also edit my index.php file, setting enviroment related stuff, etc, no problems with that.

I'd be interested to see what's faster when it comes to the redirect, in the hook or in index.php... personally I don't have time, I've got crazy gmail problems (as fuzzy knows) and a website launch due this weekend, plus my day job (which I'm at now, so couldn't even test if I wanted to!)...

If either of you end up testing what's quicker, be sure to post back here, I'd be very interested!

Anyway Ironlung, glad to hear you got it sorted, whatever the method, as they say there are many ways to skin a cat ;-)




Theme © iAndrew 2016 - Forum software by © MyBB