Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter web.config or .htaccess index.php rewrite inside a subdirectory

my website folder structure is like below...

Application Root
|- administration
|--|- application
|--|- system
|--|- index.php
|--|- web.config
|
|- application
|- system
|- index.php
|- web.config

Two CI framework installed here.

  1. site root
  2. administration folder

Here I am talking about the administration subfolder.

My web.config URL Rewrite is like below.

<rules>
         <rule name="Rewrite to index.php">
            <match url="index.php|robots.txt|images|test.php" />
            <action type="None" />
        </rule>
        <rule name="Rewrite CI Index">
            <match url=".*" />
            <conditions>
                <add input="{REQUEST_FILENAME}" pattern="css|js|jpg|jpeg|png|gif|ico|htm|html" negate="true" />
            </conditions>
            <action type="Rewrite" url="index.php/{R:0}" />
        </rule>
    </rules>

problem is, if I remove index.php from $config['index_page'] of site_root/administration/application/config/config.php, any controller function shows 404 page not found. The site root web.config actually doing this.

I want to remove index.php from URL inside the subdirectory.

1. How could I perform it through web.config ?

2. How could I perform it through .htaccess ?

I found THIS, but got no answer.

like image 865
Raja Avatar asked Oct 12 '16 16:10

Raja


2 Answers

I had the same problem but it's working now. Here's what worked for me. Fu Xu's answer was good, but it didn't quite work. So, if you make the following modifications to his code, it may work for you as it did for me.
I changed
<rule name="Rewrite Administration Index">
to
<rule name="Rewrite Administration Index" stopProcessing="true">

then I changed
<match url="administration/(.*)" />
to
<match url="^administrator/(.*)$" ignoreCase="true"/>

then
<conditions>
to
<conditions logicalGrouping="MatchAll">

and also modified this
<add input="{REQUEST_FILENAME}" pattern="css|js|jpg|jpeg|png|gif|ico|htm|html" negate="true" />
to
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />

here's the full code:

<rule name="Rewrite Administration Index" stopProcessing="true">
  <match url="^administration/(.*)$" ignoreCase="true"/>
  <conditions logicalGrouping="MatchAll">
     <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
     <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
  </conditions>
  <action type="Rewrite" url="administration/index.php/{R:1}" />
</rule>

<rule name="Rewrite CI Index" stopProcessing="true">
  <match url="^(.*)$" ignoreCase="true" />
  <conditions logicalGrouping="MatchAll">
    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
  </conditions>

  <action type="Rewrite" url="index.php/{R:1}" appendQueryString="true" />
</rule>

I hope this works for you

like image 53
Gbolahan Awoseyila Avatar answered Sep 19 '22 13:09

Gbolahan Awoseyila


I've just run a mockup as per your folder structure on a Linux System.

I added the standard htaccess file as you find in the codeigniter users guide. So there are two .htaccess files. One in the main application (root) Folder and the other under the administration folder.

Removed the index.php from $config['index_file'] ='' in both config files.

Created a Home controller in both with a whoami method that just echos back "I am the administration home controller page" for the administration Home controller called by /administration/home/whoami and "I am the main home controller" for the /home/whoami.

Both returned their appropriate messages indicating that each is being called correctly.

The .htaccess straight out of the users guide.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

So I imagine the same should work with IIS but dont hold me to that. I found something that may help there... web.config version of the .htaccess

So for the above setup you should be able to put in the appropriate urls to hit both CI Instances.

like image 20
TimBrownlaw Avatar answered Sep 20 '22 13:09

TimBrownlaw