How do I convert a query string to a segment URI?

I am trying to convert a query string;

http://atwd/books/course?course_id=CC100&format=XML&submit=Submit

      

Into the URI segment;

http://atwd/books/course/CC100/XML

      

I am working at CodeIgniter.

I've looked at stackoverflow answer that said to check out the CodeIgniter URL segment guide, but I don't think there is any information on how to convert a query string to a segment URI. There is, however, a way to convert the segment URI of the segment to a query string, which also increases the load on Google results.

Following this StackOverflow question I tried this in my .htaccess file but nothing worked

RewriteCond %{QUERY_STRING} ^course_id\=([^&]+)\&format\=([^&]+)$
RewriteRule ^$ /course/%1/format/%2 [R,L]

      

In my .htaccess file, I have this:

<IfModule mod_rewrite.c>
RewriteEngine on

#Source: http://codeigniter.com/user_guide/general/urls.html
#Removal of index.php
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php?route/$1 [L]

#Source: /questions/2231851/htaccess-get-url-to-uri-segments
#Format Course function requests
RewriteBase /
RewriteCond %{QUERY_STRING} ^course_id\=([^&]+)\&format\=([^&]+)$
RewriteRule ^$ /course/%1/format/%2 [R,L]
</IfModule>

      

This is in my Codeigniter root directory screenshot

My code in the .htaccess file doesn't work, I refresh the page and nothing happens. However, the code to hide index.php works. Does anyone know why?

+1


source to share


3 answers


The concept of "converting urls" from one thing to another is completely ambiguous, see the top of this answer for an explanation of what happens to urls when redirecting or rewriting: fooobar.com/questions/306528 / ...

There are 2 things that are happening and I'm going to take a wild blow and assume you want the 2nd as you are complaining that page refresh does nothing.


When you type http://atwd/books/course?course_id=CC100&format=XML&submit=Submit

in your browser, this request URI that is sent through the mod_rewrite: /books/course

. Your rule you are mapped to empty the URI: RewriteRule ^$ /course/%1/format/%2 [R,L]

. This is the first reason your rule is not working. The second reason why this doesn't work is because above all else, everything except images and index.php and robots.txt is routed through index.php. So even if you were mapped to the correct URI, it gets routed before your rule even does anything.

You need to adjust the pattern in your rule to match the URI that you plan to redirect, and you need to place that rule before the rule you have. So everything should look something like this:

<IfModule mod_rewrite.c>
RewriteEngine on

RewriteBase /
RewriteCond %{QUERY_STRING} ^course_id\=([^&]+)\&format\=([^&]+)$
RewriteRule ^/?books/course$ /course/%1/format/%2 [R,L]

#Source: http://codeigniter.com/user_guide/general/urls.html
#Removal of index.php
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php?route/$1 [L]

</IfModule>

      



You will need to tweak the paths to make sure they match what you are really looking for.


To redirect the browser and internally rewrite back to the original url, you need to do something different.

First you need to make sure that all your links are as follows: /course/CC100/format/XML

. Modify your CMS or static HTML so that all links display exactly like this.

Then you need to change the rules around (everything up to your codeigniter routing rule) to be something like this:

<IfModule mod_rewrite.c>
RewriteEngine on

RewriteBase /

# redirect browser to a URI without the query string
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /books/course/?\?course_id=([^&]+)&format=([^&]+)
RewriteRule ^/?books/course$ /course/%2/format/%3? [R,L]

# internally rewrite query string-less request back to one with query strings
RewriteRule ^/?course/([^/]+)/format/([^/]+)$ /books/course?course_id=$1&format=$2&submit=Submit [L]

#Source: http://codeigniter.com/user_guide/general/urls.html
#Removal of index.php
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php?route/$1 [L]

</IfModule>

      

+1


source


I'm not going to address the misunderstanding that has already been well addressed in another answer and comments, and I can't speak specifically for CodeIgniter, but by providing the routing url shortens quickly, it seems like most web frameworks do:


You probably just want to route all traffic (which doesn't match the physical files) to the frontend web controller (index.php) and handle url management in CodeIgniter routing, not the htaccess file.

For that, your htaccess can be as simple as:

<IfModule mod_rewrite.c>
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule .* index.php [QSA,L]
</IfModule>

      

This, as I said, will redirect any traffic that doesn't match a physical file like robots.txt or an image to your index.php.



Then, using routing as described in the docs ( http://ellislab.com/codeigniter/user-guide/general/routing.html ) you can take parameters and pass them to your controllers as you see matching, no need to "convert" or "display" anything, your URL does not need to be resolved /?yada=yada

internally, depending on your routing rules. CodeIgniter can work it out.

You need wildcard routes like this from the docs:

$route['product/:id'] = "catalog/product_lookup";

      

A rough example of what yours might look like looks something like this:

$route['course/:id/format/:format'] = "course/something_or_other_action";

      

0


source


If I understand you correctly, you may change your mind. I have something similar in my own code.

I have a controller named Source. In this controller, I have the following method:

public function edit($source_id, $year)
{
    # Code relevant to this method here
}

      

This gives:, http://localhost/source/edit/12/2013

where 12 refers to $source_id

and 2013 refers to $year

. Each added parameter is automatically converted to its own URI segment. It also didn't need .htaccess tricks or custom routes.

0


source







All Articles