1

I am having an issue with mod rewrite. I am also a total beginner to it. I am using CodeIgniter and trying to rewrite the following:

http://url.dev/news/news_selection?item=59

to look like this:

http://url.dev/news/news_selection/59/

I have followed a tutorial on: https://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/ (more specifically the : "Patterns and Replacements" section)

The issue is I write my RewriteRule as below and it still does not rewrite the url after going to the page (eg. http://url.dev/news/news_selection?item=59)

RewriteRule    ^news/news_selection/([0-9]+)/?$    news_selection?item=$1    [NC,L]

Any advice on where I am going wrong? Thank you.

I am not sure if the routes and controller affect this issue but I added them for reference below.

In my routes for codeIgniter I have

//_News Folder
$route['news/(:any)'] = 'pages/view2/_news/$1'; 
  • 1: _news is the folder

  • 2: news_selection is the php file

  • 3: pages is the controller

My controller has the following function for view2:

public function view2($sub ='', $page='')
  {
          if ( ! file_exists(APPPATH.'/views/pages/'.$sub.'/'.$page.'.php'))
          {
                  // Whoops, we don't have a page for that!
                  show_404();
          }

          $data['title'] = ucfirst($page); // Capitalize the first letter

          $this->load->view('templates/header', $data);
          $this->load->view('pages/'.$sub.'/'.$page.'.php', $data);
          $this->load->view('templates/footer', $data);
  }

The rest of my .htaccess:

Options -Indexes
Options +FollowSymLinks
RewriteEngine On
RewriteBase /

#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,

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

#When your application folder isn't in the system folder

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]

# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin

ErrorDocument 404 /index.php

2 Answers 2

1

You just need this to use url as above:

RewriteEngine on
RewriteCond $1 !^(index\.php|images|captcha|css|js|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

You can use this for all controller. This file .htaccess is in application folder.

Sign up to request clarification or add additional context in comments.

3 Comments

Can you please explain a little more in depth? The website works fine with: url.dev/news/news_selection?item=59 as a url. I just want to clean it up where you enter: url.dev/news/news_selection/59 and it rewrites it for passing.
I don't know much about this. But I read an article about CodeIgniter URL, if you set $config['enable_query_strings'] = TRUE; it will display an url like a query string. Because the first time I use CI, my url is normal,not like a string. You can read more at: link
Thank you for the help. This isn't the answer for me as it will break form helper along with a few other things. I think this is a different way to go about accessing your model and views via variables. I finally did figure out how I had to do it and have posted my answer below.
1

I figured it out after about 10 hours of trying everything today.

I decided that it may be a routing issue with CI and decided to try passing my variable from the url in the cleaned up format. So...

If I enter: http://url.dev/news/news_selection/59

It will work with the following route:

//_News Folder
$route['news/news_selection/(:num)'] = 'pages/view_test/_news/news_selection/$1';

and controller:

  public function view_test($sub='', $page='', $var1='')
  {
          if ( ! file_exists(APPPATH.'/views/pages/'.$sub.'/'.$page.'.php'))
          {
                  // Whoops, we don't have a page for that!
                  show_404();
          }

          $data['item'] = $var1;

          $this->load->view('templates/header');
          $this->load->view('pages/'.$sub.'/'.$page.'.php', $data);
          $this->load->view('templates/footer');
  }

By using these I can pass the page number as $var1 and then pass it on to my view via $data. Then I picked up $data['item'] as $item in my news_selection.php page and passed it to my model for processing. Reference to CI Manual for this.

After that the page processes like it normally would have.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.