htaccess routing codeigniter - .htaccess

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

Related

.htaccess file is not working in nested directory but works in parent directory?

I am creating a small eCommerce website. I want to hide the query string parameters and want to replace them with the forward-slash /. For this reason, I have created the following .htaccess file structure:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9]+)$ $1.php [L]
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)$ $1.php?product=$2 [L]
This works in the parent directory http://localhost/ecommerce/index.php?product=43:
http://localhost/ecommerce/43
But if I copy this same .htaccess file in the nested directory product to access the item http://localhost/products/product/index.php?product=43:
http://localhost/products/product/43
It did not work. I cleared cache and cookie as well but still no luck. What can be the issue in my .htaccess file? Thanks!!!

.htaccess Rewrite Rule Understanding Problems

Site Structure
/articles/Employment/Companies.php
/articles/Employment/Companies/.htaccess
/articles/Employment/Companies/index.php
.htaccess file reads
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/(.*)$ index.php [L]
So when you go to
/articles/Employment/Companies/[company type]
It is displaying the index.php page.
The Problem
I'm trying to link to
/articles/Employment/Companies.php
without the .php being displayed, however if I link to
/articles/Employment/Companies
it is going to
/articles/Employment/Companies/
What i'm Ideally Looking For
Understand why I my site is adding the / when linking to folder/hello
to strip out all .php so if you go to /hello it'll display /hello.php apart from in certain directories such as my current .htaccess file is located where /this or /that will display /index.php.
Please try with below, use from rewritecond with your existing rule what I am doing if the request is actually for php file which is not index.php then serve the extension less code.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule !index.php$ $1.php [L]

Remove index.php from url for ONLY CI subdirectory

I get how to remove index.php from CI urls overall. The following works fine:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /crm/v2/index.php?/$1 [L]
However, it interfers with other code on the live site. This new CI version i'm adding is in a subdir named v2. Thus the layout is something like:
-originalDir
--v2
-otherDirs
-.htaccess
-etc...
So in essence urls come out something like:
// Original page
https://www.site.com/originalDir/somepage.htm
// And this exist too
https://www.site.com/originalDir/index.php
// My new stuff
https://www.site.com/originalDir/v2/index.php/controller/etc...
// Desired effect withOUT affecting the original sites index.php page
// aka, the below is what i WANT but NOT getting!
https://www.site.com/originalDir/v2/controller/etc...
I can code in a lot of languages, but never a lot of experience with these htaccess rewrites. Any ideas on how to make it rewrite index.php for ONLY the codeigniter sub-directory? I've tried a few things that seem to work locally, but not on the live server. Not sure the exact structure of rewrite code.
Assuming you want to only rewrite rules in the CI directory, and not point any URLs outside of that directory to it...
Change your RewriteRule to match only URI's that start with your CI directory.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^crm/v2/(.*)$ /crm/v2/index.php?/$1 [L]
Alternatively, you can put an .htaccess file in your /crm/v2 directory, and specify a RewriteBase.
RewriteEngine on
RewriteBase /crm/v2/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
Have you tried using RewriteBase?
RewriteEngine On
RewriteBase /crm/v2
RewriteRule ^(.+)$ index.php?/$1 [L]
I have not tested this specifically with your information but this is from a template that I use for some of my projects.
Here is the documentation for RewriteBase: http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase
I hope this helps!

.htaccess. Redirect requests to ~username folder

Situation:
I'm moving a website from a production environment to a test environment.
The test environment url is similar to http://192.168.1.100/~username/
There are thousands of files which use the following within the html
<img src='/images/image.jpg' />
Since the request is going to root http://192.168.1.100/ the files are 404.
Rather than finding and replacing all of html I'd assume that there is an easy way to fix it with mod_rewrite via .htaccess.
I've tried using the following
RewriteCond %{REQUEST_URI} !^/~username/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /~username/$1
But did not work as expected.
Thanks in advance.
UPDATE
The development environment resides within cpanel/whm. So when the username is removed from the requested url, it now belongs to the root users. So, my question now: How do I update the .htaccess file for the root user to mod_rewrite back to the ~username?
If you remove
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
it appears to work as expected, because any request to the right url will not be rewritten.
you might want to add [L] as a flag to signify it's the last rewrite rule, like so:
RewriteCond %{REQUEST_URI} !^/~username/
RewriteRule ^(.*)$ /~username/$1 [L]

Having trouble using .htaccess for hiding the .php extension

I'm having trouble using .htaccess.
This is the content of my .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}!-d
RewriteCond %{REQUEST_FILENAME}!-f
RewriteRule ^([^.]+)\.php$ $1 [L]
I opened up a text file, pasted these lines and saved it as .htaccess
It was showing .htaccess before i right clicked the .htaccess file and changed it to "open with notepad". I guess that shouldn't make a difference but now its showing a blank name.
The main problem is when i open my localhost on the browser through wamp, the folder where I've kept the .htaccess file, isn't visible or if i access it shows this internal server error.Now, if i remove the .htaccess file from there, it shows up in the localhost directory and doesn't show an error when i try to open it.
If you're getting the 500 Internal Server Error, your .htaccess file is being read. From what I see, you may be missing spaces before the !:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
#-----------------------------^^
RewriteCond %{REQUEST_FILENAME} !-f
#-----------------------------^^
RewriteRule ^([^.]+)\.php$ $1 [L]
But if you want your users not to see the .php extension, the RewriteRule is backwards. The first part should not contain the .php:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# The input URL has no PHP, but internally is served as .php
RewriteRule ^([^.]+)$ $1.php [L]

Resources