CodeIgniter 2 no longer able to use PATH_INFO after modifying .htaccess - .htaccess

I'm brand new to CI and I'm trying to remove the annoying "index.php" from the URLs. This is the .htaccess I'm using:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
As you can see, I have to use QUERY_STRING to make the new URL work (note the question mark after index.php). When I try to use PATH_INFO (i.e. without the question mark), I only get No input file specified error.
This is fine, and I'm OK with using QUERY_STRING if I have to, but I don't understand the problem because I was using PATH_INFO just fine before I started playing with the rewrite - i.e. the default of "example.com/index.php/controller/function" was working, and that uses PATH_INFO AFAIK.
Does anyone know why the htaccess breaks PATH_INFO in my example? Sorry for the stupid question.

Try this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php/%{REQUEST_URI} [L]
The rewrite rules above seem to work fine for an app that I'm currently working on... they pass the correct GET requests along with the proper PATH_INFO values.

Related

Remove trailing slash from url using htaccess

i have the following rewrite condition and it works fine:
RewriteEngine on
RewriteRule ^([a-z0-9_-]+)\.html$ index.php/page/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|asset|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
However, all it takes is adding a trailing backslash to my url and the site breaks. I have seen several similar questions and answers but nothing works for me, mainly because i'm not familiar with htaccess.
How can i make make my url auto change from
http://localhost:8888/mysite/mylink/
to
http://localhost:8888/mysite/mylink
Iam using codeigniter and my application is still in localhost. Appreciate any help in this. Thanks.
Just change all your links without that slash at the end.

CodeIgniter: add custom rewrite rule to redirect from static file to controller

Lets say that I have a static app and a JSON file that feeds it.
The problem is that the JSON file it's not available yet and I can't use it, so, to make the application run in development environment, I want to add a rewrite rule to redirect the JSON request to a CI controller path that will return a JSON as well.
My htaccess file looks like this
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|assets|static|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
#Rewrite for development
RewriteRule ^static/app/json/appData.json$ /buscador/getAppData
The first condition is the CI main condition to redirect all request to index.php
Then I added the RewriteRule for the JSON file that's going to be located in static/app/json/appData.json and it needs to be redirected to the CI controller buscador/getAppData
The thing is that this Rewrite Rule isn't working and I don't know why, can somebody tell me what is wrong o how can I add a custom rewrite rule in CI?
Thank you!
I was pondering on a similar problem for quite a few hours. I needed to add a custom RewriteRile so that a shorter link should be generated (and working) in order to be sent as a footer in an SMS to users. I finally managed to do it with a 301 redirect before the CI redirects. Here's my .htaccess:
Options -Indexes +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
#Custom redirect
RewriteRule ^nv([0-9]+?)?$ /news/locate/item/$1 [NC,L,R=301]
#Remove trailing slash against SEO duplicates
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)/$ /$1 [L,R=301]
#Generic controller redirects
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Now http://domain.tld/nv1234 redirects to http://domain.tld/news/locate/item/1234 as intended, although not as transparently as I would like it to, but I couldn't find a better way.
Please note the R=301 in the Custom redirect line - the RewriteRule will not work if the client is not redirected to the newsController.
Posting my solution here with the hope that other developers will not get lost as I (and the OP) did.

htaccess mod rewrite for phpmyadmin

I have been searching with no success on getting my mod rewrite rule to work. I am creating a site like most now that use a rewrite rule to point back to a specific php file that will display content based on the url. The issue i am having, is trying to get the rewrite rule NOT to apply if the user went to "something.com/phpmyadmin" I know it has been asked before, and i have read those posts, but i still cant seem to get mine to work, here is the rewrite scripts i have, and it is still sending me to the /index.php instead of /phpmyadmin/
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/phpmyadmin
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php
If i go to /phpmyadmin/index.php it works fine, but i am really bothered why i cant get the directory part to work.
You should use THE_REQUEST variable:
RewriteEngine on
RewriteCond %{THE_REQUEST} !/phpmyadmin [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of some rewrite rules.

using - instead of _ (underscore) with .htaccess

I'm using with good results the following code to access alla of my php files into the /it directory without specifying the extension. In other words I can access to "http://www.mydomain.com/it/about.php" just writing "http://www.mydomain.com/it/about".
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://www.mydomain.com/it/$1.php [L]
the same happen when i try to access to http://www.mydomain.com/it/question_answers.php.
How can I access directly to *"http://www.mydomain.com/it/question_answers.php"* also writing "http://www.mydomain.com/it/question-answers"?
I wrote the floowing code below the previous but it seems not to work.
Redirect 301 /question-answer http://www.mydomain.com/it/question_answer.php
because if i write "http://www.mydomain.com/it/question-answer" the browser try to open the page:
"http://www.mydomain.com/it/question-answer.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php"
A small abstract of the post:
I have the page *"http://www.mydomain.com/it/question_answers.php"*
with the first part of code I can get it using the link *"http://www.mydomain.com/it/question_answers"*
I'd like to access the same page also with the following "http://www.mydomain.com/it/question-answers"
Thanks!
This should work for you:
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://www.mydomain.com/$1.php
RewriteRule it/question-answer\.php http://www.mydomain.com/it/question_answer.php [R=301,L]
I have changed two things: I deleted it/ in the new URL of the first RewriteRule. Otherwise you would be redirected to it/it/
I also added \.php to the second RewriteRule. I don't really know why, but the RewriteRule seems to replace the pattern instead of redirecting. And if your pattern is it/question-answer and the real url is it/questions-answer.php the .php will not be replaced.

Expression Engine - htaccess to hide index.php is not working. Why?

I have an MSM install with four (licensed) sites. Three of them work perfectly and behave as they should. The fourth one, currently under construction seems to have a mind of its own. The home page shows up but attempts to add additional templates or template groups with content do not display. I only get
The requested URL /template-name/ was not found on this server.
I double checked and made sure the Enable Strict URLs was set to No and the templates are all synched properly. At this point I am repeating myself. Any clues?
Edited
I found out that if I insert index.php into the URL the other pages and templates will show, which leads me to believe that I have something wrong with the htacess file.
Here is the code I am using (which has worked just fine for other sites):
# BEGIN ExpressionEngine Rewrite
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteRule ^(.*)$ /index.php?$1 [L]
</IfModule>
# END ExpressionEngine Rewrite
Any clues why this is not working correctly?
Turns out it was the htaccess file. I replaced the rewrite noted above with the following and it solved the problem:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]

Resources