Codeigniter site replicated in subdirectory not displaying individual pages - .htaccess

We have just copied a Codeigniter website (from our root domain) to a subdirectory on the same root domain (with all config.php changes made and a new database etc.). However, page links don't display the individual pages yet the browser simply reloads the index.php page content (as if it's loading a new page) and changes the URL in the browser window to the correct page.
This is really odd and I've spent hours pouring over it, so I'm hoping someone here may be able to give me a starting search point.
For your information, the .htaccess in the subdirectory is:
RewriteEngine on
RewriteRule ^test.php$ mod_rewrite.php
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

if theres a new codeigniter install in a subdirectory now that used to be on the root, you need to include the name of the sub directory in front of the index.php parts of your htaccess
something like this(the change is on the last line):
RewriteEngine on
RewriteRule ^$ /dev/index.php [L]
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico|zip\.php|resources)
RewriteRule ^(.*)$ /dev/index.php?/$1 [L]
dev should be changed to the name of your subdirectory. also, if neither or these works and you have a htaccess on your root, it could be effecting this subdirectory.

This is now sorted after a lot of different attempts!
File config.php in root -> application -> config (line 46) was originally:
$config['uri_protocol'] = ($_SERVER['SERVER_ADDR'] == '127.0.0.1') ? 'AUTO' : 'ORIG_PATH_INFO';
It should be:
$config['uri_protocol'] = ($_SERVER['SERVER_ADDR'] == '127.0.0.1') ? 'AUTO' : 'PATH_INFO';
All's now functioning correctly.

Related

Htaccess for redirecting a directory to a file with the same name

After doing a quick lookup on how to manage sites with multiple language support, I find site.com/language/page/ the neatest url layout. (as I don't have the funds for site.language)
I have used htaccess to redirect the base site from site.com to site.com/language/
by using: RedirectMatch ^/$ /language/ where 'language' is language.html
But since I have a directory called /language/ so that all other pages in the given language can be put inside it, the site just shows up as the index of the directory.
How can I accomplish that kind of layout, if I want the main index page to show up in the url as site.com/language/ ?
Current htaccess that worked fine until I added the directory:
## Rewrite Defaults
RewriteEngine On
## Remove file extension + force trailing slash
RewriteCond %{REQUEST_URI} (.*)/$
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule (.*)/$ $1.html [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule .* %{REQUEST_FILENAME}/ [R=301,L]
## Redirect to /en-gb
RedirectMatch ^/$ /en-gb/
With the root folder looking like:
/language
stuff.html
morestuff.html
language.html
...
I would really appreciate help as this is currently a nightmare situation. When I try compiling the htaccess with answers to similar questions, everyone has just parts of what I am looking to achieve and thus I break the layout with every modification...
Is it possible to change the back-end filenames and accomplish this layout using only htaccess for front-end url rewriting since the url bar is the only thing that matters?
If I understand correctly, you should be able to solve this by removing the line:
RewriteCond %{REQUEST_FILENAME} !-d
Let me know if that works.

Codeigniter sub-folder multiple controllers

Here is my htaccess file and it works somewhat. Not matter what controller I specify it always goes to the home page
<IfModule mod_rewrite.c>
RewriteEngine On
#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
#This last condition enables access to the images and css folders, and the robots.txt file
RewriteCond $1 !^(index\.php|css|js)
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
This is my website URL
http://automationmetrics.local/automation/
Thoughts?
Some things I usually check when this happens to me:
Have I enabled the mod_rewrite module in Apache?
Have I set $config['index_page'] to blank?
If the above works, here's the one that I use, that's working on my end:
https://gist.github.com/petrepatrasc/6925413
If you're STILL out of luck, then try fiddling with the $config['uri_protocol'] parameter - I remember that I could only get it to work on Windows (with IIS at the time) using REQUEST_URI as a value. Might be related to that.
The first two rules get triggered on all the files, just like it says in the comment. So this one:
RewriteCond $1 !^(index\.php|css|js)
seems redundant and removing it should solve the issue you're having.

can't remove index.php with .htaccess

i have installed codeigniter and i want to remove the index.php from url when i access localhost/aplication/index.php. i have set uncomment mod_rewrite.so from httpd.conf en this is my .htaccess file :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
its succes when i acces url from localhost/aplication/index.php/data/users/12 but not with this url localhost/aplication/data/users/12.
how can i fix this??
Try to follow this page about codeignitor pretty urls. Your rules look correct, but I'm terrible with regex.
Some things to look into would be checking if mod_rewrite is available by using phpinfo();. Also step three in that link talks about enabling mod_rewrite using a2enmod. And make sure you restart Apache.
check your config file there should be a line that says:
$config['index_page'] = 'index.php';
You can either set this variable to just '' or you can comment this line out completely, either way it is part of the process of removing the index.php from your codeigniter urls.
this is how you remove the index.php:
htaccess file in the root folder:
RewriteBase /
RewriteEngine on
RewriteCond $1 !^(index\.php|images|js|css|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
for managing some static pages like: example.com/about , example.com/contact, example.com/terms etc
got to config routes file and edit:
//the controller that will be when it is exampl.com
$route['default_controller'] = 'YOUR_MAIN_CONTROLLER_NAME_HERE';
//I creted this for pages that are created dynamically or for displaying error when I want
$route['404_override'] = 'friendlyPages';

Htaccess rule to make urls like this ?page=1 look like /page/1 in Codeigniter

This is how my urls currently look:
http://mysite.com/?page=1
How can I make this work?:
http://mysite.com/page/1
There is a post on StackOverflow that asks the same question. But the accepted solution isn't working for me. Because I am using Codeigniter and my page results in a 404 perhaps because since the url pattern of a CI site is:
domain/controller/method
The system is assuming that I am requesting a controller called "page" and a method called "1" both of which of course doesn't exist. Or maybye it's due to a conflict with the other code in my htaccess file (which I downloaded from the CI wiki, it gets rid of index.php and does a few security things). Here is my entire htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#Removes access to the system folder by users. Additionally this will allow you to create a System.php controller, '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. 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]
#Pretty urls for pagination links
RewriteRule (.*) index.php?page=$1
</IfModule>
The non indented bit is the solution I got from that other SO question that isn't working for me.
Any solutions to this CI pagination issue?
UPDATE
Ok, read some of the docs and now I have this working:
http://mysite.com/home/index/2
What would be the htaccess rule to turn that into?:
http://mysite.com/page/2
You should make this configuration at /application/config/routes.php (and let the .htaccess just for hide the index.php as you are already doing).
$route['page/(:any)'] = 'home/index/$1';
Or better, like #zaherg remembered (ensures that only numbers could by matched):
$route['page/(:num)'] = 'home/index/$1';
This way all the requests to http://mysite.com/page/2 will be treated internally as http://mysite.com/home/index/2 and so forth.
I suggest you take a look at CodeIgniter User Guide - URI Routing and CodeIgniter User Guide - Tutorial − Introduction.
Good luck.
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
from CodeIgniter docs
That will handle removing the index.php, but what happens after that depends how CodeIgniter's query string handling is set up: it can be configured to use a query string rather than a path. See the link for more details.

Cannot remove index.php from my urls in Codeigniter

I am using codeigniter and when I set up the website I added necessary code in my .htaccess file to remove index.php (default) from my urls. I just happened to replace my public_html folder from a back up file of my website. Since I did that, I cannot get any of the links on home page to work unless insert index.php in the url between the domain name and the rest of the url as in http://www.obsia.com/index.php/contact/ instead of http://www.obsia.com/contact/ and it works. So, in my config file within the application folder, I changed
$config['index_page'] = "";
to
$config['index_page'] = "index.php";
and all the links seem to work now. But how do I remove index.php from the URLs.
Here is what my .htaccess code looks like:
RewriteEngine on
RewriteBase /
RewriteCond $1 !^(index\.php|public|user_guide|robots\.txt|css)
RewriteRule ^(.*)$ /index.php?/$1 [L]
Can anyone point me in the right direction. It will much appreciated.
Regards,
G
What yo want to do can be done this way:
RewriteEngine on
RewriteBase /
# Static content. Rewrite to - (don't change) and mark as last rule.
RewriteRule !^(index\.php|public|user_guide|robots\.txt|css) - [L]
# Do the rewrite.
RewriteRule ^(.*)$ /index.php?/$1 [L]
However, a slightly better way of doing that is this:
RewriteEngine on
RewriteBase /
# Only do the rewrite under the condition that the requested URL isn't a (real) file.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?/$1 [L]
there are so many ht access files... In my case i was editing the wrong file... u should edit the file that is inside the project.. for example i work on code igniter and project name was test , so inside test folder i had a htaccess file and inside my application folder ie test/application there was another htaccess file.. i was editing the access file inside the application folder, but i had to edit the file in test folder not test/application/.htaccess.... hope sum 1 finds this useful.. cos i spent half a day to figure this out :(
Cheers
ksp

Resources