CodeIgniter Forums
What are the chars that the url can include? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: What are the chars that the url can include? (/thread-53895.html)



What are the chars that the url can include? - El Forum - 08-13-2012

[eluser]Leo78[/eluser]
Hey,

I am creating a search in my website, the address has to be in this pattern:

Code:
www.domain.com/news/search/query/
www.domain.com/news/search/query/2/

The first pattern is when you are in page number 1 and the second pattern is when you are in page number 2+.

So I created the following code in routs.php:

Code:
$route['news/search/(:any)'] = 'news_ci/sorting/search/$1/1';
$route['news/search/(:any)/([1-9]+)'] = 'news_ci/sorting/search/$1/$2';

The problem is for example the user types www.domain.com/news/query/char/ then he gets a SQL error, I'm pretty sure it's because I use slash in the end of the address. so instead writing (:any) I want to write a pattern which includes all (:any) pattern includes, without the slash.


What are the chars that the url can include? - El Forum - 08-13-2012

[eluser]Aken[/eluser]
It probably doesn't have anything to do with the slash. First, your routes are backwards. The most specific ones should be first. Even if your URI matches the second example, it will always hit the first one and stop there. So flip those two around.

Second, your number regex is excluding zero. So if a user ever tries to go to page 10 for example, the route won't match. I would just use (:num) instead, and make sure your controller checks that number.

Third, it sounds like you're using something from the URI in your SQL query. Avoid doing that wherever possible - always make sure to put it in the format you need before using it in a query. If it needs to be a number, typecast it as an integer first.


What are the chars that the url can include? - El Forum - 08-15-2012

[eluser]Leo78[/eluser]
1. Do you mean the first should be (:num) and then (:any)? In this case it's ok, but in other cases it's not what I prefer, because of SEO.. isn't there another solution?

2. Right.

3. Yes I am. (:num) - page number, (:any) search query. But I check it before I insert it to the SQL query so it's fine.

Thank you, and if you have a solution to the order of the parameters please hit me.


What are the chars that the url can include? - El Forum - 08-15-2012

[eluser]CroNiX[/eluser]
1. No, he means the route with 4 segments needs to come before the route with 3 segments.

Code:
$route['news/search/(:any)/(:num)'] = 'news_ci/sorting/search/$1/$2';
$route['news/search/(:any)']        = 'news_ci/sorting/search/$1/1';



What are the chars that the url can include? - El Forum - 08-15-2012

[eluser]Leo78[/eluser]
It doesn't solve the problem, but I think I have another direction.

The route of my article page is:

Code:
$route['news/(:any)'] = 'news_ci/article/show/$1';

Code:
http://www.domain.com/news/name-of-article/
http://www.domain.com/news/name-of-article/asd/wew/sda/ ..

Both of these address work, when I need that just address no.1 will work.

Maybe there is a problem with the .htaccess file? I found this code in the net and used it (and don't see a problem with the code):

Code:
RewriteEngine On

    RewriteBase /

    #Removes access to the system folder by users.
    #Additionally this will allow you to create a System.php controller,
    #previously this would not have been possible.
    #'system' can be replaced if you have renamed your system folder.

    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)/$ /index.php?/$1 [L]
    
    #When your application folder isn't in the system folder
    #This snippet prevents user access to the application folder
    #Submitted by: Fabdrol
    #Rename 'application' to your applications folder name.

    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)/$ /index.php?/$1 [L]

    #Checks to see if the user is attempting to access a valid file,
    #such as an image or css document, if this isn't true it sends the
    #request to index.php

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ index.php/$1 [L]



What are the chars that the url can include? - El Forum - 08-15-2012

[eluser]Aken[/eluser]
Has nothing to do with your .htaccess - it's all on your routes and application.

$route['news/(:any)'] needs to come last, after the search routes. Again, MOST SPECIFIC routes FIRST, then in decreasing order of specificity.


What are the chars that the url can include? - El Forum - 08-15-2012

[eluser]Leo78[/eluser]
[quote author="Aken" date="1345087048"]Has nothing to do with your .htaccess - it's all on your routes and application.

$route['news/(:any)'] needs to come last, after the search routes. Again, MOST SPECIFIC routes FIRST, then in decreasing order of specificity.[/quote]

I know, that what I've done. this is the full route:

Code:
// Tag, $tag, $page
$route['news/tag/(:any)/(:num)'] = 'news_ci/sorting/tag/$1/$2';
$route['news/tag/(:any)'] = 'news_ci/sorting/tag/$1/1';

// Search, $query, $page
$route['news/search/(:any)/(:num)'] = 'news_ci/sorting/search/$1/$2';
$route['news/search/(:any)'] = 'news_ci/sorting/search/$1/1';

// Archives, $year, $month, $page
$route['news/archives/(:num)/(:num)/(:num)'] = 'news_ci/sorting/archives/$1/$2/$3';
$route['news/archives/(:num)/(:num)'] = 'news_ci/sorting/archives/$1/$2/1';

// Article page
$route['news/(:any)'] = 'news_ci/article/show/$1';

// Homepage, $page
$route['news/(:num)'] = 'news_ci/sorting/newest/$1';
$route['news'] = 'news_ci/sorting/newest/1';

Again, when I enter to domain.com/news/article-name/bla/blu/ it recognizes that it should take 'article-name' as the parameter, but why doesn't it display error 404 because of the continue of the address? very strange for me. the address should be just domain.com/news/article-name/ I don't wanna the continue and don't know how to prevent it.


What are the chars that the url can include? - El Forum - 08-15-2012

[eluser]Aken[/eluser]
Because you're using (:any). That means any characters, including /. If you don't want to include another slash, you have to specify only the characters that you're going to use in the segment for your titles. Something like this will use only letters, numbers and the dash (-) char.

Code:
$route['news/([a-zA-Z0-9\-]+)'] = 'news_ci/article/show/$1';



What are the chars that the url can include? - El Forum - 08-15-2012

[eluser]Leo78[/eluser]
[quote author="Aken" date="1345092026"]Because you're using (:any). That means any characters, including /. If you don't want to include another slash, you have to specify only the characters that you're going to use in the segment for your titles. Something like this will use only letters, numbers and the dash (-) char.

Code:
$route['news/([a-zA-Z0-9\-]+)'] = 'news_ci/article/show/$1';
[/quote]

Good, that what I asked at the first post. thank you.

I think it's a kind of bug, (:any) shouldn't include the $config['url_suffix'] char/s because then these cases causes (I thought at first that it doesn't include it, but I was wrong). don't you think?


What are the chars that the url can include? - El Forum - 08-15-2012

[eluser]Aken[/eluser]
Definitely not a bug. :any means :anything, which should include, well, anything. Hah. Sorry you didn't understand that, but it's very literal.