I am new to url re-writng and is currently facing problems with rewriting url for multiple applications in codeigniter. I have scanned the whole stackoverflow with some potential answers but it is still not solving my problems entirely
My directory at the root is as such
-application
--administrator
--frontend
What I am looking for is for my users to access my web site (frontend) with http://www.mydomain.com/ And my admin (backend) as http://www.mydomain.com/admin/
my current .htaccess is as such
<IfModule mod_rewrite.c>
RewriteEngine On
# If the user types just "admin".
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^admin$ administrator.php [L,QSA]
# If the user enter in any admin section, like "admin/section".
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^admin\/(.*)$ administrator\.php/$1 [L,QSA]
# If the user types any site section, like "site/section".
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index\.php/$1 [L,QSA]
ErrorDocument 404 /index.php
</IfModule>
code from CodeIgniter multi-application .htaccess problem
I can get what I desire at the front end with the above .htaccess, but I can't get it working on my back end. Basically, www.domain.com/admin gives a 404 page not found error.
Please advice me on the problem. Also, please guide me on where I should place this .htaccess file? at the root? inside the application folder? or inside the frontend and administrator folder respectively.
Many thanks.
Regards.
Why not something like this in your root directory.
RewriteEngine on
RewriteCond $1 !^(index\.php|update\.php|assets|admin|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ index.php/$1 [L]
This will basically ignore any directories such as "admin" to route to the frontend application. You will have to set your config.php and remove index.php from the controller.
Found a way to work with this problem already. It is not a very smart way of doing it but it at least worked.
First, I created an 'admin' folder in the root, with a copy of the application's index.php
Which effectively make my directory looking like this.
-admin
--.htaccess
--index.php
-application
--frontend
--administrator
-index.php
-.htaccess
For this solution you will need to have two .htaccess files in different location.
For the .htaccess at the root, it should look like this.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|update\.php|administrator\.php|assets|admin|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
For the .htaccess in the admin folder
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/(.*)$ index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
While this works on localhost, it is noteworthy that it may not work on rented servers because the file system in your host may be different.
Related
I have a small mvc application where I'm trying to force the url to go from www.example.com to www.example.com/public.
In order to do this, I have a .htaccess file in the root (/) and then another .htaccess within the /public folder to point to index.php with friendly urls. I believe the issue is within the htaccess file that sits in the public folder, I need all requests to go through index.php
I have tried a few threads on stackoverflow but seems to give me a server 500 error when I access www.example.com
.htaccess of root
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /public/
</IfModule>
.htaccess of public folder
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}% !-f
RewriteCond %{REQUEST_FILENAME}% !-d
#allow images, css and js links
RewriteCond %{REQUEST_URI} !\.(css|js|jpe?g|gif|png)$ [NC]
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
</IfModule>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule (.*) /public/index.php
I'm using MAMP and CodeIgniter.
The root of my website is: /Users/Roy/Websites/CodeIgniter-3.0.2
In the Websites folder I also have some other project but I don't think that matters. Here is the apache httpd.conf file: http://pastebin.com/Am0ew0C0
In my .htaccess file I'm using the following:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
<IfModule authz_core_module>
Require all denied
</IfModule>
<IfModule !authz_core_module>
Deny from all
</IfModule>
the .htaccess file is located at: /Users/Roy/Websites/CodeIgniter-3.0.2/.htaccess
I have no idea why the mod_rewrite isn't working, it's supposed to eliminate the use of index.php in the url because the URL I have to use now is: http://localhost:8888/CodeIgniter-3.0.2/index.php/about
And I would like that to become: http://localhost:8888/CodeIgniter-3.0.2/about PHP version 5.6.10 is used. the .htaccess file is not being read by apache, that is the problem here, how to fix it?
Problem solved, there is no need to mess with the httpd.conf with MAMP installed. Simply get CodeIgniter and place it in the htdocs folder inside MAMP. After that create a .htaccess file inside the CodeIgniter folder (not the application folder inside there) and use the following code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /ci/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Now you have clean URL's and no longer need to use /index.php/about but can just do /about.
It maybe from the fact of what you set your document root to /Users/Roy/Websites/ in your httpd.conf file. Personally I would use virtualhosts if you plan to have multiple websites so that each one can have it's own .conf and you can set the Document root specifically for each site.
Anyway, so right now your CodeIgniter-3.0.2 is a subfolder of Websites which is your document root. So most likely you're having a problem with the rewrite base.
If you have your files and .htaccess in the codeigniter folder, your .htaccess will need to look like this.
RewriteEngine On
RewriteBase /CodeIgniter-3.0.2
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
You can use this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /Project_Name
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
It look like file permission issue. check out the properties of your .htaccess file and change the permission.
The try with this code.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
for more try http://w3code.in/2015/09/how-to-remove-index-php-file-from-codeigniter-url
.htaccess could be very frustrating. take a lot at what really works
1.Your .htaccess should look like this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /booking/
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
2.Replace line 3 with the root directory name of your project, in my own case 'localhost/booking/'
3.Copy the .htaccess file from the application folder to the root directory. This means that you will now have to instances of .htaccess file in you entire project.
4.in your config.php , Base url should look like this in my own case
$config['base_url'] = 'http://localhost/booking/'
5.
$config['index_page'] = '';
6.
$config['uri_protocol'] = 'REQUEST_URI';
7. turn on Mod-rewrite in your httpd.conf file by removing the # preceding this line
LoadModule rewrite_module modules/mod_rewrite.so
I hope this solves your problem
Good day,
I am using a framework that uses a .htaccess file to forward all requests to a sub file core/index.php
I used the following .htaccess file code :
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ core/ [L]
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) core/$1 [L]
</IfModule>
This worked great I thought until I noticed that the pages got loaded multiple times.
And this is what I want to prevent.
So I had a little look at this forum.
And I found a solution that the space between (.*) and core could be the reason.
Hence I removed it.
And to my delight the multiple db inserts stopped.
However now I have found that my requests do not get forwarded correctly.
Only the main request (www.myapplication.com/) gets forwarded correctly (index.php)
Whenever I am adding something like : (www.myapplication.com/admin) he bugs out and cannot find the page.
I hope anyone could tell me what to modify in this code :
RewriteEngine on
RewriteRule ^$ core/ [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) core/$1 [L]
Thanks in advance!!
I also note that whenever I change the (.) core/$1 [L] line to (.)core/$1 [L] that not only my (sub)pages stopped working but for the index.php the load times of my index.php file where split in half.
Hence my page truly gets reloaded multiple times.
Edit
As pointed out by a question below the system also uses a .htaccess file inside the /core directory.
This file has the following contents
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
Edit
I think it would come handy to make a small summary. If requested for a file or path I want the server to check if it is
available on the server.
www.myapplication.com/flower.jpeg and the server can find it it should display the flower.jpeg file.
If the server CAN NOT find the flower.jpeg file I want the server to forward the request to /core/index.php?url=flower.jpeg
I am using two .htaccess files
in the **public directory** (main directory)
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ core/ [L]
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) core/$1 [L]
RewriteCond %{REQUEST_URI} !core/
</IfModule>
Second file in the core directory
core/
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
The issue: my pages are in a loop hence the page index.php get's executed with every server request. E.G. 10 images on a page means 10
requests extra. Same for js and CSS files.
edit:
Solved!!!
This is the solution!!
DirectoryIndex core/index.php
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ core/index.php?url=$1 [QSA,L]
</IfModule>
You have to add
RewriteCond %{REQUEST_URI} !core/
to the RewriteCond block to prevent redirection from the target to the target.
It would be better if your target would be more specific, for example if you could specify /core/ instead of core/ (do you have a core subfolder in every folder?). Also, you will have to exclude image folders.
Try this in /core/.htaccess:
ErrorDocument 404 default
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+)/?$ index.php?url=$1 [QSA,L]
I found a solution. The working code is :
DirectoryIndex core/index.php
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ core/index.php?url=$1 [QSA,L]
</IfModule>
I have installed my wordpress installation on my server root e.g mydomain.com & codeigniter installed in subdirectory named ci e.g mydomain.com/ci. When I try to access my wordpress site it works fine, but when I try to access any of my ci controllers e.g mydomain.com/ci/signup it throw 404 Page Not Found CI error. but it worked fine for my default home controller. I have searched for solution & tried many of them but any one of them is not worked in my case.
here is the rewrite rules in .htaccess for my both installtion.
mydomain.com/.htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
mydomain.com/ci/.htaccess
RewriteEngine on
RewriteBase /ci
RewriteCond $1 !^(index\.php|user_guide|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
I think the problem is with wordpress .htaccess file. We should have to route the request that is for ci to ci folder.
This code might help, add this to wordpress .htaccess file
RewriteRule ^$ ci/$1 [L]
RewriteRule ^(.*) ci/$1 [L]
i have added my project on free hosting 000webhost and my all files are listed in public_html/ directory.
project is built in codeigniter.
i have added htaccess file at public_html/.htaccess which contains
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
but this does not working for me is there any issue in htaccess?
I use this (it works for me in both, locally and remotely):
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|public|images|css|js|robots\.txt)
RewriteRule ^(.*)$ index.php?/$1 [L]
You need to create a folder called "public" in the root rolder and sub-folders for css, js etc.
Sorry from all my htaccess is ok but i had an extra chracter in my base url which was causing problem.
Sorry again.