Htaccess file - 404 page not found on some folders - .htaccess

I am using codeigniter and I keep having these 404 errors in my log file.
ERROR - 2016-04-30 16:41:15 --> 404 Page Not Found: Assets/js
ERROR - 2016-04-30 16:41:15 --> 404 Page Not Found: Assets/js
ERROR - 2016-04-30 16:41:18 --> 404 Page Not Found: Assets/js
So every page load causes one entry of the above error in the log file. My htaccess file looks like below. What's going wrong here?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
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]
RewriteCond $1 !^(index\.php|assets|myadmin|robots\.txt)
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
I can confirm assets/js are valid folders.

Are you able to get to your assets folder by going through the web path? e.g. http://localhost/assets (try to retrieve a file if you dont have indexing enabled in your webserver e.g. http://localhost/assets/css/style.css), does it load?
If this is loading you are possibly calling your assets without using a base path which means a view is trying to call directly from within its own folder.
Using site_url() is a really good step (url helper)... I urge you to make your life easier in calling assets by making a simple helper and adding it to auto load containing (something like) this:
if (!function_exists('assets'))
{
/**
* Base asset directory call
*/
function assets($var)
{
$CI =& get_instance();
$CI->load->helper('url');
return site_url('assets') . '/' . $var;
}
}
You would then call it in your view:
<!-- load CSS -->
<link href="<?php echo assets("css/bootstrap.min.css") ?>" rel="stylesheet">
It would then show this once rendered:
<link href="http://localhost/assets/css/bootstrap.min.css" rel="stylesheet">
This is just a really basic example though, if you have heaps of assets to call in a header or footer, I would suggest creating a helper to load based on a config file, you can also specify what is required on certain page loads that way as well so its dynamically loaded with a foreach.
Also do be careful calling Assets (capital letter) vs assets (no capital letter) is very different, many webservers are case-sensitive...
I run the following for my htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond $1 !^(index\.php|assets|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

Related

htaccess of front controller seems to block images, JavaScript, and CSS

I have a problem regarding my .htaccess file. It is placed in the root folder of my site together with the index.php (my front controller) and the folders regarding CSS, JavaScript, and images. The following is the URL-related content of my .htaccess file
# skip existent files
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule index.php - [QSA,L,C]
RewriteRule .* - [QSA,L]
# protect PHP files from the outside
RewriteCond %{REQUEST_URI} ^.*\.php$
RewriteRule ^.*\.php$ - [R=404,L]
RewriteCond %{REQUEST_URI} ^img/*$
RewriteRule ^img/*$ - [QSA,L]
# refer root to index.php
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^$ index.php [QSA,L]
# redirect 404 for non existent files
RewriteCond %{REQUEST_URI} ^(.*)\..*$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)\..*$ - [R=404,L]
# adjust the rest of the domains
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?site=$1 [QSA,L]
When I then open the website in the browser, it does not load the respective resources. If I click on "show image source", it immediately refers to the 404 page.
Do you have any idea which part I need to adjust to work around this problem? The root structure looks as follows
root
-.htaccess
-/css
-/js
-/img
-index.php
Thank you very much in advance! :)
EDIT 10/08/2021:
I figured out that the problem was stemming from other RewriteRules which I put into the last section right before RewriteRule ^(.*)$ index.php?site=$1 [QSA,L], e.g.:
RewriteRule ^study-programmes/(.*)$ index.php?site=study-programmes&faculty=$1 [L]
However, I have several pages where I need specifically named parameter (such as faculty in the example above). What exactly do I need to adjust in order to make it work?
The image matching rules are not written correctly. The . is missing from the rule.
RewriteCond %{REQUEST_URI} ^img/.*$
RewriteRule ^img/.*$ - [QSA,L]

.htaccess issue | Codeigniter

I'm relatively new to Codeigniter and MVC. But, have successfully made two apps 'Locally'. While exploring, I found a way to remove 'Index.php' from the URL and also about custom routes. The .htaccess file that i have works like charm locally, but when trying to host it; there is a issue 505 internal server issue
Here is the first .htaccess code that i have (works locally) :-
<IfModule mod_rewrite.c>
# Turn on URL rewriting
RewriteEngine On
# If your website begins from a folder e.g localhost/my_project then
# you have to change it to: RewriteBase /my_project/
# If your site begins from the root e.g. example.local/ then
# let it as it is
RewriteBase /
# Protect application and system files from being viewed when the index.php is missing
RewriteCond $1 ^(application|system|private|logs)
# Rewrite to index.php/access_denied/URL
RewriteRule ^(.*)$ index.php/access_denied/$1 [PT,L]
# Allow these directories and files to be displayed directly:
RewriteCond $1 ^(index\.php|robots\.txt|favicon\.ico|public|assets|css|js|images)
# No rewriting
RewriteRule ^(.*)$ - [PT,L]
# Rewrite to index.php/URL
RewriteRule ^(.*)$ index.php/$1 [PT,L]
</IfModule>
When i use the code above there is an error. And, the app works only when .htaccess is removed. (I then have to use the inconvenient long URLs)
After a brief research and using different .htaccess without success, i asked one of my friends who has a Hosted CI app successfully running. He sends me a file which leads me the landing page without any problem; but, cant call any functions with/without using routing .i.e. If i use the custom routed URl (www.mySite.com/contact) then also it leads me to the landing page, the same with actual URL scheme (www.mySite.com/welcome/contact_page)
The new code here:-
<IfModule mod_rewrite.c>
RewriteEngine On
# !IMPORTANT! Set your RewriteBase here and don't forget trailing and leading
# slashes.
# If your page resides at
# http://www.example.com/mypage/test1
# then use
# RewriteBase /mypage/test1/
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
Using this code shows me the landing page but i cannot navigate any further from there. When i try to call a function the landing page reloads. This, I think is because of the last error Handling Code (ErrorDocument 404 /index.php)
Does anyone know what the solution the problem might be??
questions
Why doesnt the first .htaccess code work when hosted?
What may be the issue with the second available .htaccess file??
Do you guys have better .htaccess file? If yes, can you post it here??
Try this .htaccess (Codeigniter Recommended )
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|image|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
or
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
In application/config/config.php
$config['base_url'] = 'http://stackoverflow.com/';
$config['index_page'] = ''; # Should be empty
And make sure Controller name, Model names are in proper way. Bcz Linux Host is an Case-Sensitive.

htaccess routing codeigniter

I have a problem with my routes. I have my images in sources/ folder but when the image is missing it cause to load the website and future problem with system overloading.
Htaccess:
RewriteCond $1 !^(index\.php|robots\.txt|sources|uploads|captcha|sitemap\.xml|_gapi|robots\.txt|googleaac809c6bcbeb4e8\.html|googlebaf6b56ae3013092\.html|feeds|temp)
RewriteRule ^(.*)$ /index.php?/$1 [L,QSA]
This should cause to load files from those folders - working well by this point
But when there is image missing for example:
<img src="uploads/non_existing_image.jpg"> the image wont load but when I copy the URL the website will load correctly and after some time my server ends up with 500 Error.
Is there any way how to solve this in htaccess/php ?
I want to end up with 404 error on that link
Don't use .htaccess for this, just Add the index.html file to your images folder also add the index.html in CSS and JavaScript folder. When someone try load you folder index.html is auto load and didn't allow your images etc.
It will give result like this
i also use a htaccess file with codeigniter, even though mine is quite smaller then yours:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
to explain: RewriteEngine On enables the rewriting of the url,
the RewriteCond %{REQUEST_FILENAME} !-f tells htaccess that the requested file should not be an existing file,
the RewriteCond %{REQUEST_FILENAME} !-d tells htaccess that the requested file should not be an existing directory.
this way you dont need to exclude all the seperate files and/or extentions and/or directories.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
Just add this to your .htaccess file

CodeIgniter Assets Not Found

Please help with CodeIgniter assets. I tried most of the solutions from StackOverflow (including the autoload utility helper), nothing works so far. Here's my setup below:
Folder Path:
.htaccess
<IfModule mod_rewrite.cs>
RewriteBase /hnk_ci
RewriteEngine On
RewriteCond $1 !^(index\.php|assets|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
When I view source code, it looks like this:
<link href="http://localhost/hnk_ci/assets/css/bootstrap.min.css" rel="stylesheet">
But when click on the css link from source code, I get this error:
The requested URL /hnk_ci/assets/css/bootstrap.min.css was not found on this server.

CodeIgniter relative links without base_url()

My site is on "localhost/mysite" but when I view it on the browser, layout and links are broken. CSS and JS files are on "localhost/mysite/themes/" but this is what I see in the source code:
<link href="/themes/new/css/style.css" rel="stylesheet"/>
<script src="/themes/new/js/scroll.js"></script>
And when I click on the links above, I always get to the following addresses and they are broken:
http://localhost/themes/new/css/style.css
http://localhost/themes/new/js/scroll.js
"mysite" path is already missing. It should be:
http://localhost/mysite/themes/new/css/style.css
http://localhost/mysite/themes/new/js/scroll.js
On htdocs\mysite\application\application\config\config.php, I have this setting and it should be left unedited.
$config['base_url'] = '';
$config['index_page'] = '';
On htdocs\mysite.htaccess, I have this setting, and it's probably the file that needs changes.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /mysite/
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>
I looked around and tried all the suggestions by changing .htaccess but nothing really helped to make it work. It's funny when I change
href="/themes/new/css/style.css
to
href="".base_url()."themes/new/css/style.css"
everything works perfectly. But this is not the solution. It's a big site and I shouldn't be editing too much files by adding base_url() function, etc. Maybe we can do something on .htaccess? :(

Resources