I want to implement a search in codeigniter using the search term in the url string but am having trouble allowing disallowed uri characters (' is the main problem)
e.g. www.example.com/search/find/search_term/collector's edition/category/stackoverflow
basically find 'collector's edition' in category 'stackoverflow'
This throws the URI exception error - even if I encode it with javascript codeigniter unencodes it. Obviously I don't want to go and allow all characters.
I also want to be able to decode my data when it is returned so I can display the search term in the input box also.
Use a query string rather than fight against CI's suggested allowed URI characters:
example.com/search/?search_term=collector's+edition&category=stackoverflow
Just make sure you have query strings ($_GET) enabled:
$config['allow_get_array'] = TRUE; // This enables $_GET data
// The name of this item is misleading, it's not what you might think
$config['enable_query_strings'] = FALSE; // <-- Ignore this, make sure it's FALSE
And to grab the search term:
$query = $this->input->get('search_term'); // No need to decode
You need to add “=” to your allowed_uri_chars. You’ll find that string in your config/config.php file.
It could also be the . (dot) in there. Try adding a dot instead. You need to be a little bit brave
and experiment to figure out what works.
Try $config[‘permitted_uri_chars’] = ‘a-z 0-9~%.:?=_\-’;
Related
We are using Server.URLEncode to change an SKU with a forward slash from BDF5555/45 to BD5555%2F45 in our Href on a button.
When a person clicks on the button the page navigates to another module which has Request.QueryString but DNN is changing the URL.
How can I get the variable decodeprodCode to include the &45 as BDF5555/45?
Perhaps DNN is rewriting the URL?
There is a NavigateURL class in DotNetNuke.Common.Globals that will generate a correct url based on a TabID and a lot of overloads.
DotNetNuke.Common.Globals.NavigateURL(TabId)
You can also use querystring parameters
DotNetNuke.Common.Globals.NavigateURL(TabId, ControlKey,"key=value"))
DNN by default will re-write querystring values into /key/value format in the URL assuming that it is properly encoded. For example if you have querystring values of sku=1 and productid = 2 the resultant URL will be
https://yoursite.com/Your-Page/sku/1/productid/2
This is done via the FriendlyUrlProvider, but it should not have any impact to your ability to process via Request.Querystring as this is a very, very common practice for passing values into DNN.
Your code to retrieve the value is correct.
I ended up using the following code instead of a Request.Querystring.
string RawurlFromRequest = Request.RawUrl;
var cleanSKU = RawurlFromRequest.Split(new[] {"sku/"}, StringSplitOptions.None)[1];
var CleanSKUNoOtherQueryStrings = cleanSKU.Split(new[] {"&"}, StringSplitOptions.None)[0];
The Request.RawURL brings back the URL with the special characters as it is without encoding. As Mitchel Sellers mentioned above, DNN uses the FriendlyURLProvider which rewrites the URL.
For example www.mysite.com/ProductFilter/SKU/BDF5555/45 and not
www.mysite.com/ProductFilter/SKU/BDF5555%2F45
The CleanSKU variable will look for SKU/ and split everything on the left as it is set to [1].
After everything has been split on the left, we look for other QueryStrings which we usually add with a & sign. We will split everything on the right by setting it to [0].
This will bring back BDF5555/45 in the backend with a forward slash which we can use to retrieve product information from our ERP system which URLdecode couldn't do.
I have a URL such as:
http://www.example.com/something?abc=one&def=two&unwanted=three
I want to remove the URL parameter unwanted and keep the rest of the URL in tact and it should look like:
http://www.example.com/something?abc=one&def=two
This specific parameter can be anywhere in the URL with respect to other parameters.
The question looks very simple, but I tried many times but failed in the end.
The entire query string is present in the $args variable or at the end of the $request_uri variable. You will need to construct a regular expression to capture everything before and after the part you wish to delete.
For example:
if ($request_uri ~ ^(/something\?.*)\bunwanted=[^&]*&?(.*)$ )
{
return 301 $1$2;
}
See this document for more, and this caution on the use of if.
In few pages I need to make pagination. This of course can be achieved with URI segments, but in few cases in addition to pagination parameters, I need to pass some other GET parameters for filtering purposes.
So obviously in this case i need to be able access the controller via query string like so:
example.com/?c=controller&m=function
In order to achieve this I set the enable_query_strings to TRUE in main config file.
This seemed to work, but I discovered that it breaks a bunch of different stuff. For example if I use current_url() the URL returned has a ? at the end to accommodate the query string. So if I use it in form, it does not work.
So is there any way to enable the controller access to controller functions only to specified functions?
You can construct you url like this:
/param1/param2/pagination_parameters
So you will be able to send custom data (number of params), and pagination data using just URI segments.
For example if I use current_url() the URL returned has a ? at the end
to accommodate the query string. So if I use it in form, it does not
work.
Please note, that you can also left form 'action' blank, so result will be the same as if you had used current_url() (http request will go to the same script).
Instead of enabling enable_query_strings in main config, just enable in pagination config only. So it will apply to that particular page only.
Ex:
$config['page_query_string'] = TRUE;
This question has been asked a few times but I can't seem to find a solution that helps me which is why I am trying here.
I have my site setup with the following for URLs I am using CodeIgniter I have a controller called user which loads a user view.
So my URLs are structured as follows:
http://example.com/user/#/username
I want to try and strip out the user controller from the URL to tidy up my URL so they would just read:
http://example.com/#/username
Is this possible I have been looking at route and have tried lots of different options but none have worked?
$route['/'] = "user";
Could anyone offer any solution?
Assuming the '#' in your URLs is a valid function and 'username' is a parameter for that function, then this route should work:
$route['#/(:any)'] = "user/#/$1";
Depending on what usernames are to be routed you may want to change the wildcard. For example, if you only wanted to route numbers as the parameter, you could change (:any) to (:num).
(:num) will match a segment containing only numbers.
(:any) will match a segment containing any character.
You can also use regular expressions to define routing rules, allowing you to further restrict what is routed.
I have a page that has this category URL website.com/category/view/honda-red-car and I just want it to say http://website.com/honda-red-car no html or php and get rid of the category view in the URL.. this website has been done using the CodeIgniter framework..
also this product view URL website.com/product/details/13/honda-accord-red-car
and I want it to be website.com/honda-accord-red-car PLEASE HELP!!!
I cannot find correct instructions on what I am doing wrong??
In Routes.php you need to create one like so
$route['mycar'] = "controller_name/function_name";
So for your example it would be:
$route['honda-red-car] = "category/view/honda-red-car";
Take a look into the URI Routing part of the user guide.
If you have concrete set of urls that you want to route then by adding rules to the application/config/routes.php you should be able to achieve what you want.
If you want some general solution (any uri segment can be a product/details page) then you might need to add every other url explicitly to the routes.php config file and set up a catch-all rule to route everything else to the right controller/method. Remember to handle 404 urls too!
Examples:
Lets say the /honda-red-car is something special and you want only this one to be redirected internally you write:
$routes['honda-red-car'] = 'product/details/13/honda-accord-red-car';
If you want to generalize everything that starts with the honda- string you do:
$routes['(honda-.*)'] = 'product/details_by_slug/$1'; // imaginary endpoint
These rules are used inside a preg_replace() call passing in the key as the pattern, and the value as the replace string, so the () are for capture groups, $1 for placing the capture part.
Be careful with the patterns, if they are too general they might catch every request coming in, so:
$routes['(.*)'] = 'product/details_by_slug/$1';
While it would certainly work for any car name like suzuki-swift-car too it would catch the ordinary root url, or the product/details/42 request too.
These rules are evaulated top to bottom, so start with specific rules at the top and leave general rules at the end of the file.