Codeigniter index.php breaking image paths - .htaccess

When I don't edit my .htaccess file, my image path reads something like: http://this.website.com/codeigniter/inc/images/logo.jpg.
However, as soon as I add this code into the .htaccess file:
RewriteEngine On
RewriteCond $1 !^(index\.php)
RewriteRule ^(.+)$ index.php?$1 [L]
(to remove the index.php from the URL), I get 404 error, even with the exact same path.
How do I remove index.php and still have access to my images?

RewriteEngine On
RewriteCond $1 !^index\.php [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?$1 [L]
The two new RewriteConditions would exclude existing directories and files (and so images) from redirection.

Related

.htaccess to access the file but to ignore non existing directory in URL path for that file

I have my htaccess and it already does couple of good things (it hides www and .php in my url) but I also need to add one additional feature to it.
I have the main domain called: wdd.ie
I have a root folder called: wdd.ie
Most important files are kept in the root.
I have also the following file in the root: about.php
I love to access about.php file by calling non existing directory: www.wdd.ie/myweb/
And my full URL path looks like this: www.wdd.ie/myweb/about
I would like to access the file (stored in the root) to allow non existing directory in url but to ignore it
Could anyone help!? Please, please help!
As this new desired htaccess feature complexity is now probably beyond my skills.
<IfModule mod_rewrite.c>
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ / [L,QSA]
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
</IfModule>
If I've understood correctly you want the following url www.wdd.ie/myweb/about to offer up content from www.wdd.ie/about.php but keep the url the same?
If so, then the following should work
RewriteRule ^myweb/about$ /about.php [L]

How can I hide folder name from URL using .htaccess

I have a website with the root folder 'www', but I put all php files including index.php in a sub-folder of root.
I wrote myself a .htaccess file to redirect, so if I input www.test.com, it will jump to www.test.com/folder and display the index.php.
Below it's my .htaccess which I put in the root.
RewriteEngine On
RewriteBase /
RewriteRule ^folder2 - [L]
#ignore folder2 in which I put important files, but no works for the website
RewriteCond %{REQUEST_URI} !^/folder(.*)
RewriteRule (.*) /folder/$1
Right now, I want to change the .htaccess file to reach these goals:
When I input www.test.com, jump to www.test.com/folder as usual, but display the url without folder.
All the pages in folder will display the url without folder name.
Such as
www.test.com/shop/page1 -> www.test.com/page1
I searched some of the scripts, but none of them works.
I've found this related question which answers to your problem. The snippet from the answer is (note: it's adjusted to your needs):
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+folder/([^\s]+) [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?!^folder/)^(.*)$ /folder/$1 [L,NC]
Reference: https://stackoverflow.com/a/18361995/3673491
You can use below code.
RewriteEngine On
RewriteBase /
RewriteRule ^/?$ /folder/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /folder/$1 [L]
Based on your current rules. You can just make a slight adjustment.
RewriteEngine On
RewriteBase /
#ignore folder2 in which I put important files, but no works for the website
RewriteRule ^folder2/? - [L]
RewriteRule ^/?$ /folder/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /folder/$1 [L]

.htaccess rule not excluding existing files when rule has a slash condition

I am redirecting this type of url
http://www.site.com/windows/games/ or http://www.site.com/windows/games
http://www.site.com/windows/software/ or http://www.site.com/windows/software
to
http://www.site.com/windows/ct.php?ct=games
http://www.site.com/windows/ct.php?ct=software
Site structure
wamp/www/Site/windows/ct.php
I am trying this way its redirecting properly but when url has trailing slash at end its rewriting css, js files too.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^windows/([^/]*)/?$ /Site/windows/ct.php?ct=$1 [L]
i.e
this type of url has css and javascript applied.
http://www.site.com/windows/games
but this type of url has no css and javascript applied. (not working with trailing slash).
http://www.site.com/windows/games/
I have tried several syntax like
RewriteRule ^windows/([^/]+)/?$ /windows/ct.php?ct=$1 [L]
RewriteRule ^windows/(^(.[^/]*)/?)$ /windows/ct.php?ct=$1 [L]
RewriteRule ^windows/([^/]*)/? /windows/ct.php?ct=$1 [L]
RewriteRule ^windows/([^/]+)/? /windows/ct.php?ct=$1 [L]
but it didn't work.
Complete .htaccess
Options +FollowSymlinks -MultiViews
RewriteEngine on
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^windows/([^/]*)/?$ /Site/windows/ct.php?ct=$1 [L]
RewriteRule ^[^/]*/[^/]*/(.*\.html) /Site/error/400/ [L]
RewriteRule ^(error) - [L]
RewriteRule ^sitemap\.xml$ sitemap.php [L]
RewriteRule ^rss/(.*?)\.xml$ rss/$1.php [L]
</IfModule>
ErrorDocument 404 /Site/error/404/
Please see and suggest any possible way to do it.
Thanks.
The issue here is that your relative paths break when you add a trailing slash as it introduces two-levels of directory structure while the CSS/JS file paths come out only once ... So, your RewriteRule is actually firing for CSS/JS files as well when resolving 404s for paths like /windows/global/js/js.js. But, the scripts still fail to work because they aren't under /Site and that's where your rule is serving the request from.
So, to fix things without touching the relative URLs or resource locations; add another rule as follows:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^windows/([^/]*)/?$ /Site/windows/ct.php?ct=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^windows/(global/.*)$ /$1 [L] # handles css|js files
The rest of your .htaccess (of course) remains the same.
I use the following rule in .htaccess, but this may be too general:
RewriteRule ^q/([^/]+)/([^/]+)(\/?)$ /path/to/q.php?type=$1&q=$2&{QUERY_STRING} [NC]
This may be better for you:
RewriteRule ^windows(\/?)$ /path/to/windows.php?{QUERY_STRING} [NC]
RewriteRule ^windows/([^/]+)(\/?)$ /path/to/windows.php?type=$1&{QUERY_STRING} [NC]
You will have to change this according to your situation. I haven't tested this, but I hope it is good enough for you to get where you want.

Remove .php extensions with .htaccess without breaking DirectoryIndex

I have the following rewrite in my .htaccess file which removes the .php extension from files, converting for example so.com/question.php to so.com/question.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
However this also breaks the default DirectoryIndex behaviour, in which just typing the directory will redirect to the index file in the folder, e.g. so.com/answer displays so.com/answer/index.php
Simply combining the above code with DirectoryIndex index.php does not achieve both results.
Can someone help me combine these two functions, or rewrite the code to exclude index.php files, which would achieve the same result?
I'm thinking you just need to verify that the file exists prior to doing the rewrite, that way you'll leave 404 and directoryindex behaviours intact:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
(not tested)
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
I tested and it is working fine.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [L, QSA]
verify files and folder and also, add RewriteBase /

.htaccess clean URL and relative paths

I have the following in my .htaccess which is :
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /index.php?type=cat&id=$1 [QSA,L]
RewriteRule ^page/([^/]+)$ /index.php?type=page&name=$1
This seems to work just fine but relative paths to pictures and css files inside of index.php become broken in the second case (Page). did not work. In second case, all images are pointing to page/images/ instead of image/
Other than hardcoding the actual path to images, is there any other way to fix this?
images, css, js folders are located in the root. This is how the root looks like
.htaccess
index.php
images/
css/
js/
RewriteCond directives only apply to the rule directly following them.Try the follwing
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /index.php?type=cat&id=$1 [QSA,L]
RewriteCond %{REQUEST_URI} !\.(css|js|jpe?g|gif|png)$ [NC]
RewriteRule ^page/([^/]+)$ /index.php?type=page&name=$1 [L]
#rewrite requests for page/images to images
RewriteCond %{REQUEST_URI} ^/page(/images/.+)$ [NC]
RewriteRule . %1 [L]
EDIT.
Modified to rewrite page/images to images

Resources