Codeigniter issue accessing /admin - php

I have a script that was written using codeigniter.
When accessed using a subdomain http://name.domain.com/admin/ it works fine but I now need to have the script installed in a folder and I cannot access http://domain.com/name/admin/ i just get a 404
I would guess (but I could be totally wrong) that it is a .htaccess issue.
RewriteEngine on
RewriteCond $1 !^(index\.php|install|updates|backups|images|css|js|uploads|jPicker|ckeditor|kcfinder|qr|fancybox|test\.php|licence\.txt|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
I would really appreciate any help to point me in the right direction so I can resolve this issue.
Thanks
{EDIT1}
With NO .htaccess file name.domain.com/, name.domain.com/admin & domain.com/name all work, it is only domain.com/name/admin that does not work.
When I add a .htaccess as above or the one in the second answer below the subdomain URL's still work but domain.com/name stops working and domain.com/name/admin continues not to work

You should exclude also your name folder on your .htaccess:
RewriteEngine on
RewriteCond $1 !^(index\.php|install|updates|backups|images|css|js|uploads|jPicker|ckeditor|kcfinder|qr|fancybox|test\.php|licence\.txt|robots\.txt|name)
RewriteRule ^(.*)$ /index.php/$1 [L]

Related

How should I configure redirects for multisite Codeigniter app?

I have multiple codeigniter sites setup like so:
/CI_Site1
/CI_site1/CI_Site2
/CI_site1/CI_Site3
Right now to use my the second and and third site I have to do it like so:
mydomain.com/CI_Site2/index.php/controller/function
If I don't put the index.php then it throws up a 404 in the root site(AKA CI_Site1).
How should I configure htaccess or apache site conf or CI config files such that I don't have to add index.php?
I think the first step should be to configure the routes in CI_site1 to not process requests for the other 2 but I don't know how. Here is hoping a Codeigniter pro can help me.
Thanks in advance.
EDIT: I know how to remove index.php from URLs with htaccess. But that is not the issue here. How do I prevent CI_site1 from processing requests that are for CI_site2 and CI_site3?
Just add below code in file and save it as .htaccess then upload it to Project folder
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/system.*
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?/$1 [L]
Make sure your server mod_rewrite is on.
Did you try using routes?
$route['CI_Site1'] = '/CI_Site1';
$route['CI_Site2'] = '/CI_site1/CI_Site2';
$route['CI_Site3'] = '/CI_site1/CI_Site3';
I realized that no extra configuration was required. It is sufficient to use .htaccess to remove index.php. My issue was that apache was blocking .htaccess files in its default configuration. Editing apache2.conf to allow processing of htaccess files resolved my issue.

Rewrite/hiding some URL in .htaccess

I'm trying to get right syntax for .htaccess without any result...
I've a URL structured as domain.com/app/public/pageName .
It's working fine but I would "hide" the 'app/public/' part in browsers, basically doing something like:
[real URL] domain.com/app/public/pageName -> domain.com/pageName [what users type and see in browsers]
I think in that way it should be more readable and seo-friendly.
As I understood from docs (and maybe it's wrong because it's not working...) I should tell to Apache to map/redirect all URL like domain.com/pageName to domain.com/app/public/pageName , but only internally, in order to show the minimal URL in users' browsers.
Right now I have something like:
RewriteEngine on
#RewriteBase /app/public/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ https://localhost/app/public/index.php?url=$1 [QSA,L]
(I'm using full URL with https://... in order to get something that will be quick and easy to adapt when I upload all to my hosting, is it right?).
Problem is that RewriteRule actually change the URL, because it perform a redirect and URL rewrite it's not handle internally.
So, first of all: is it possible what I'm trying to do? If so, how can I handle the URL rewrite only internally?
Everything should be uploaded to a shared hosting, so I don't have other than .htaccess.
Anyway, I can consider to upgrade to a vps if there are not other possibilities...
Thanks!
==============
EDIT (should be more clear now)
tl;dr version:
I'm looking for a method that let users to type domain.com/pageName (and they will see that address in their browsers) and rewrite internally that URL in order to point to domain.com/app/public/pageName.
==============
More: after /app/public/ there can be an arbitrary number of elements, separated by / . All of these elements are appended at the end of the URL after index.php. At the end URL looks like:
domain/app/public/index.php?url=lot/of/elements/here
This is already working with the RewriteRule posted above, I would keep that too.
Thanks!
This is working fine for me, Hope it will work for you as well.
Check .htaccess here
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} ^/app/public
RewriteRule ^app/public/(.*)$ /$1 [L,QSA]
Just for reference, I found a solution, maybe will be usefull for someone.
Basically I moved .htaccess to the root server (instead of /app/public directory) and changed the RewriteRule as follow:
RewriteEngine on
RewriteBase /app/public/
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [PT]
Now it's working (at least on localhost).
What do you think? Are there any side effects with this config?

URL Rewriting returns 500 internal server error

I am trying to convert this:
site/read.php?id=6
to
site/read/6
I have tried a couple of solutions found on SO, with the last one being (to output: site/read/id/6):
RewriteRule ^(.*?\.php)/([^/]*)/([^/]*)(/.+)? $1$4?$2=$3 [NC,N,QSA]
When I try the second link, it will hang, and apache crashes (LOL).
Not sure if it has a problem with the rest of the .htaccess file, so here is the full code:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^(.*?\.php)/([^/]*)/([^/]*)(/.+)? $1$4?$2=$3 [NC,N,QSA]
I could achieve (with rows 1-4) that no php extensions are showing up, so the address bar currently reads site/read?id=6
Can you please point me out where have I gone wrong?
You can put this code in your htaccess (which has to be in root folder)
Options -MultiViews
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/read\.php\?id=([0-9]+)\s [NC]
RewriteRule . /read/%1? [R=301,L]
RewriteRule ^read/([0-9]+)$ /read.php?id=$1 [L]
I would do dthis in PHP , it is very hard to debug what Apache is getting , it may be that it goes into indefinite loop, did you find anything in apache log files ?
But save yourself a trouble and do it in PHP, if you are using a Framework it is easy to inlcude one PHP that checks for this and redirects to new page if it detects .php in url.

htaccess: three variables and two different pages

I am currently trying to figure out how to create an htaccess file that will allow me to perform the following, however I have searched everywhere, and on stackoverflow but I cannot find a solution.
I have an existing site, with a directory /brands/, and an htaccess file setup so that putting anything after brands e.g. /brands/diesel redirects to /brands/index.php?brand=diesel. What I now need to do is redirect anything further in the URL to a seperate page ( in the same directory ), for example /brands/diesel/jeans/male will redirect to inner.php?sear=diesel&t=jeans&g=male
I have tried the following with no success:
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ inner.php?sear=$1&t=$2&g=$3 [L]
Anyone who is competent at writing htaccess files will probably be able to spot what is wrong immediately, but unfortunately I am not great with these.
Any help would be greatly appreciated.
I think you might have to setup RewriteBase.
RewriteBase /brands/
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ inner.php?sear=$1&t=$2&g=$3 [L]
Try these rules
RewriteEngine On
RewriteBase /brands/
RewriteRule ^([^/.]+)/([^/]+)/([^/]+)/?$ inner.php?sear=$1&t=$2&g=$3 [L,QSA]
RewriteRule ^([^/.]+)/([^/]+)/?$ inner.php?sear=$1&t=$2 [L,QSA]
RewriteRule ^([^/.]+)/?$ inner.php?sear=$1 [L,QSA]

Rewritten URL shows 404 errors

I have re-written my URL from website.com?id=1 to website.com/1 and I'm getting 404 errors when trying to access the page and cannot think of a solution to this. I'm currently developing a link shortener. This is required so users will be able to access their shorted links.
This is my current .htaccessfile
RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /(index\.php)?\?id=([0-9]+)([^\ ]*)
RewriteRule ^ /%3?%4 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)/?$ /?id=$1 [L,QSA]
I cannot figure out whether this has something to do with the .htaccess file or if I need to add something else to my php code.
Would someone have some sort of idea? Thanks.
You need to explicitly rewrite back to index.php in your second rule. By the time rewrite rules are processed the DirectoryIndex directive has already been processed (or may never be processed at all - it depends a little on your virtual host configuration and in what scope the DirectoryIndex directive was declared).
The end result of this is that you need to explicitly rewrite the request to the script that you want to handle the request, you can't just rewrite it to the root of a directory. Try changing your second rewrite rule to:
RewriteRule ^([0-9]+)/?$ /index.php?id=$1 [L,QSA]
On a personal note, it's interesting to see someone else use the %{THE_REQUEST} approach to this problem, this is an idea that I myself only recently came up with, although presumably I am not the first to do so. For the benefit of future visitors, here is a related post that explains why this requirement would come about and the thinking behind it.
I think you have written wrong rewrite rules.
They must be something like this:
for example.com/website.php?id=x..
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=([^/]+)$
RewriteRule ^website\.php$ %1/ [L]
as discussed here: https://stackoverflow.com/a/4951918/2274209
Hope this will solve your query.

Categories