How to separate same directory - controller name in Codigniter - .htaccess

I've got a controller called course and a directory with the same name, and when I request
my_site/course
it redirects my to the directory course Instead of controller, how can I redirect to the controller
P.S It's very necessary to keep directory with the same name in my apllication

Does the directory need to be int he root folder, can it not be further down the hierarchy? By having a directory with the same name as your controller in the root directory, your web server is serving up the directory instead of normal CodeIgniter routing.
If it's an absolute must to have the folder in the root, then you would need to change your .htaccess file to ignore requests which match the directory name for that directory (and any others which match controller names).
There's an answer for this already, as well as a good example on the Drupal website which I've put in below in case it gets moved
=========[ start of .htaccess snippet]==========
<IfModule mod_rewrite.c>
RewriteEngine on
#
RewriteCond %{REQUEST_URI} !^/yourDirectoryName
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L,QSA]
</IfModule>
====================[ end ]=====================

There nativ Ci routing.
In config/routes.php
$route['course'] = 'controller/course';
after default routes

Related

htaccess configuration directories for subdomains and root domain

My folders in my www directory as set up as follows:
www/forums
www/helpdesk
www/www
In the base www folder (not www/www), I have the following set up in my htaccess file to redirect based on subdirectory
# Direct subdomains to appropriate folder in WWW directory
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
I also have the following set up for configuring the index.php on the main site (www.example.com) - this is also in the htaccess file in the www directory:
# Rewrite rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ www/$1 [L,QSA]
The problem I am facing is that I have stuff from forums.example.com that I want to embed in www.example.com (Vanilla Forums + WordPress plugin) - if I configure this using the admin panels, the iFrame gets blocked because they are different domains.
I found out that when I go to www.example.com/forums, I get the same front page as forums.examples.com - but all the clean URLs break.
When I look at the .htaccess file in the www/forums folder, I see the following configuration
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php\?p=$1 [QSA,L]
What do I need to do to the .htaccess file in the www directory so that when I go to www.example.com/forums, it works the same as if I went to forums.example.com
...the following set up in my htaccess file to redirect based on subdirectory
# Direct subdomains to appropriate folder in WWW directory
Aside: That snippet is just a canonical non-www to www redirect. It doesn't "redirect based on subdirectory" nor does it "direct [any] subdomains". (?)
the iFrame gets blocked because they are different domains.
It sounds as if you need to set an Access-Control-Allow-Origin HTTP response header on forums.example.com to allow the content to be "embedded" in example.com? Something like the following (using mod_headers) in the www/forums/.htaccess file:
Header set Access-Control-Allow-Origin "http://example.com"
What do I need to do to the .htaccess file in the www directory so that when I go to www.example.com/forums, it works the same as if I went to forums.example.com.
How do "all the clean URLs break"? However, given the HTTP header mentioned above, you shouldn't have to do anything more and still access forums.example.com (not the subdirectory).
Incidentally, the fact you can access the subdirectory (that the subdomain points to) is just how your hosting is configured. Normally you should block access to the subdirectory in order to prevent duplicate content issues (and any other issues, such as the linking problem you mention).
Also note, that due to the way mod_rewrite directives are inherited (or not in this case). The mod_rewrite directives in the www/forums/.htaccess file completely override the mod_rewrite directives in the parent folder.

Codeigniter 2 shared hosting sub-directory installation

I know this has been asked a lot of times before, but I did search a lot without finding an answer. So I'm hoping someone will help me out. I have a shared hosting and inside the public_html folder I wish to install my CI app. The sub-directory name is "hrms".
So I uploaded all the CI files to hrms. Now when I visit http://www.example.com/hrms/index.php/login/do_login I get the famous error No input file specified. I have tried with .htaccess file with several variations that I could find, but none have helped. The http://www.example.com/hrms/index.php is working, so I'm guessing all the config is all right.
What am I doing wrong? Also, is the .htaccess file a must, even if I intend to access the site via full URL?
==== .htaccess =====
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /hrms
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /hrms/index.php?/$1 [L]
#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /hrms/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 ^(.*)$ /hrms/index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>
Much ado about nothing.
===== UPDATE =====
I've now discovered that the problem is with GoDaddy hosting (GoDaddy and CI have a hate-hate relationship, it seems). The trick is to define your index.php as index.php? (notice the question mark) in the config.php file.
Nevertheless, many thanks to everyone who put effort into it!
Please note: If you're fine with ugly URLs like http://www.example.com/index.php?/HomeController/myFunction and aren't using routing (like me for now), there is no need for the .htaccess file.

What should I write in .htaccess file to rewrite this url?

I have a server parked on xyz.com. I have Wordpress installed on xyz.com/ and xyz.com/blog. I have created a new directory xyz.com/mamba.
In xyz.com/mamba. I want what that if user visits xyz.com/mamba/hello then the url should be rewritten to xyz.com/mamba/index.php?message=hello.
What should I write in .htaccess file in xyz.com/mamba/ directory?
Try:
RewriteEngine On
RewriteBase /mamba/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?message=$1 [L]
The first line turns on the rewrite engine for the /mamba/ directory. Without doing this, the rules won't get applied (and the rules in the parent directory gets applied instead). The RewriteBase tells the rules here that any relative URI in the target should have /mamba/ as a base URI. The 2 conditions say that the request must not point to an existing file or directory, and the rule rewrites the request and puts it in the message query string parameter for index.php.

Load new site from subdirectory as index

I want to load my new website from subdirectory including default index.html page.
In my public_html, I have /oldsite/ and /newsite/ folders...
Then.. When I access to http://www.mysite.com/.. i want it to load all contents from http://www.mysite.com/newsite/ without redirecting. I want to do it with .htaccess mod_rewrite if possible..
Can anybody help me out with this. How to do this?
# to ensure that server already know that you going to use mod-rewrite
RewriteEngine on
# if the request from http is mysite.com or www.mysite.com go to next line (else abort)
RewriteCond %{HTTP_HOST} ^(www.)?mysite.com$ [NC]
# if the request destination is not the folder /newsite go to next line
RewriteCond %{REQUEST_URI} !^/newsite/
# if the requested name is not an existed file in public_html directory
RewriteCond %{REQUEST_FILENAME} !-f
# if the requested name is not an existed directory in public_html directory
RewriteCond %{REQUEST_FILENAME} !-d
# forward request to /newsite/
RewriteRule ^(.*)$ /newsite/$1
# if the request domain is mysite.com (with out any string afterward), go to next line
RewriteCond %{HTTP_HOST} ^(www.)?mysite.com$ [NC]
# forward request to the default file under newsite directory
RewriteRule ^(/)?$ newsite/ [L]
It is generally not a good idea to deploy a website like this. Instead you should create virtual hosts for the live and development versions of your site. If you can't do that with your current hosting provider you can make use of symlink for public_html and when the new version is ready to go live just change its path. Hope that helps.

Storing domain files in subfolder

I'm often updating my website through various design iterations, and want to simplify my life by putting each version in its own folder: ie: www.mysite.com/v1.
How can I store all the contents of my in that folder (/v1, /v2, etc) yet have it accessed by simply typing in www.mysite.com.
I don't want just want to redirect the url, I want to remove the v1 from the url entirely.
Assuming you're using Apache, you could use mod_rewrite for this. Simply create a .htaccess file in the root of your public directory with a simple rewrite rule:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) v2/$1 [L]
</IfModule>
This will rewrite all URL's to the v2 directory. If you update your site to a new version, simply change v2 into something else and all requests will be rewritten to that directory.

Resources