CodeIgniter Forums

Full Version: URL-encoding in redirect?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Christoffer[/eluser]
Hey,

I am building a search function and I need to create easy to read URLs for bookmarks etc. This is a Swedish application first which means we need to support some special characters (å, ä, ö) that are not supported in URLs.

Therefore I need to encode the variable before it's posted to the user. The PHP function rawurlencode() works when I print the variable with echo() but neither redirect() nor header("Location: x") uses the encoded value.

Why is this and how can I work around it?

Code:
<?php
  // THIS WORKS!: echo rawurlencode((string)$query);
  $keyword = rawurlencode((string)$query);

  // This won't
  redirect('/search/' . $keyword );
?>

Thank you!

El Forum

[eluser]cindor[/eluser]
hi,

you can use 2-way encryption algorithms. by using CI's enrypt library you can first encrypt the encoded string and then in your search method first decrypt the string then decode that string.

El Forum

[eluser]Christoffer[/eluser]
That might work, but the link wouldn't be easily readable which I prefer. The same function would be used for some hyperlinks that needs to be SEO-optimized.

El Forum

[eluser]cindor[/eluser]
for SEO you can create a replace function which replaces unsupported chars with SEO-optimized ones. but this way you may not be able to query the database. if i were you i would send 2 parameters.

/search/seo-optimized-query-string/enrypted-query-string

and i would use the decrpyted and decoded third parameter for querying the database

El Forum

[eluser]Christoffer[/eluser]
That was an interesting idea. I'll go with that Smile

Thanks a bunch!