.htaccess DirectoryIndex not working - apache

I have a website running on Linux/UNIX but I don’t know a darn thing about the .htaccess file and the syntax used in it..
If you go to http://pr.bananapages.net the system won’t produce any results.
But if you add the file name, it comes up. ex: http://pr.bananapages.net/index.html
NOTE: The URL points to a subfolder on the shared access hosting server but it doen't really matter because when you type in www.bananapages.net/pr/hamshack, (That's where the short cut DNS address points to), It still won't produce the index.html file without actually specifying it.
I have tried everything I could find about DirectoryIndex but I can’t get it to work without using the file name… (index.html)
This is my current .htccess file.
Options -MultiViews
DirectoryIndex system.php index.php
<IfModule mod_rewrite.c>
DirectoryIndex system.php index.php index.html
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)\.(.*)$
RewriteRule ^(.+[^/])$ http://%{HTTP_HOST}%{REQUEST_URI}/ [L,R=301]
# Google sitemap controller
RewriteRule ^sitemap.xml$ tmp/sitemap.xml [L]
RewriteRule ^tmp/sitemap.xml$ tmp/sitemap.xml [L]
RewriteRule ^index.html$ index.html [L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*)(\.xml|\.php([0-9]*)|\.tpl|\.phtml|\.ini|\.inc|/)$ system.php?_p=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ system.php?_p=$1 [QSA,L]
</IfModule>
# compresses text, html, javascript, css and xml
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript
AddOutputFilterByType DEFLATE application/xml application/xhtml+xml application/rss+xml
AddOutputFilterByType DEFLATE application/javascript application/x-javascript
</IfModule>
NOTE: If I rename the .htaccess file, it will work without specifying the filename, (index.html) so. I am fairly certain there is something in this .htaccess file preventing this from working the desired way.

You can try this:
in all your
RewriteCond %{REQUEST_FILENAME} !-f
Double it as thus, to say to do rewrite only if lt's neither directory, nor file:
Edit
(Tested in Debian/Apache2).
Options -MultiViews
DirectoryIndex system.php index.php index.html
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)\.(.*)$
RewriteRule ^(.+[^/])$ http://%{HTTP_HOST}%{REQUEST_URI}/ [L,R=301]
# Google sitemap controller
RewriteRule ^sitemap.xml$ tmp/sitemap.xml [L]
#RewriteRule ^tmp/sitemap.xml$ tmp/sitemap.xml [L]
#RewriteRule ^index.html$ index.html [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)(\.xml|\.php([0-9]*)|\.tpl|\.phtml|\.ini|\.inc|/)$ system.php?_p=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ system.php?_p=$1 [QSA,L]
to see if it works better? Please notice some commented infinite loop rewriterule.

I had a similar situation on my vps
I wanted my default homepage to be login.php so if someone where to go to http://[ip]/[folder]/ it would just load the login.php file that I set in my .htaccess within the [folder]
so i set 'DirectoryIndex login.php' in .htaccess but
it would not work instead I would get a 403 when going to http://[ip]/[folder]/ as I didn't have any index.html or index.php
so...
I decided to check the apache config file httpd.conf and under DirectoryIndex I put 'login.php' as part of the list that was already there then restarted the httpd service and
it worked for me!

Here's the answer...
# Rules for subfolders
# for pr.bananapages.net
RewriteRule ^pr/(.*)$ pr/$1 [L]
# for jm.bananapages.net
RewriteRule ^jm/(.*)$ jm/$1 [L]
# and so on....
RewriteRule ^dr/(.*)$ dr/$1 [L]
RewriteRule ^usvi/(.*)$ usvi/$1 [L]
RewriteRule ^bvi/(.*)$ bvi/$1 [L]
RewriteRule ^kitts/(.*)$ kitts/$1 [L]
RewriteRule ^ar/(.*)$ ar/$1 [L]
RewriteRule ^tt/(.*)$ tt/$1 [L]
I don't know why that works but it does seem to work...
Dale

Related

.htaccess file wont redirect to /public folder (laravel issue)

I have folder /var/www/html/project/himp and there is my laravel installation.
In /var/www/html/project is my landing page index.html and some css files.
Inside /himp folder I have .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
also in /himp/public folder I have also .htaccess:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Now when I go to the domain.com/himp/public I get Laravel installation and startup screen but when I go to the domain.com/himp I get just folder views, so there is no redirection to public folder ...
Why? What can be a problem here? Please help.
Contrary to the pattern in RewriteRule (see "What is matched?"), the variable REQUEST_URI contains the full path, including /himp. So the condition should be either
RewriteCond %{REQUEST_URI} !/public
without a beginning of string anchor ^, or you must include the full path
RewriteCond %{REQUEST_URI} !^/himp/public

After htaccess changes the css stop working as stylesheet

I have a weird issue with my css files.
After i changed the htaccess file to this
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{THE_REQUEST} /eventDetails\.php\?id=([0-9]+)&name=(.+)\sHTTP [NC]
RewriteRule ^ /%1/%2? [L,R]
RewriteRule ^([^/]+)/([^/]+)/?$ /eventDetails.php?id=$1&name=$2 [L]
</IfModule>
my css files stoped loading to ALL the pages of the website.
My public_html folder contains
index.php
profile.php
css -> style.css
js -> owl.carousel.js
.htaccess
With Google Chrome Developer Tools i have this error for the css files
Resource interpreted as Stylesheet but transferred with MIME type text/html: "http://something.gr/css/style.css".
and this error for the javascript files
owl.carousel.js:1 Uncaught SyntaxError: Unexpected token <
Some images from the Network Tab.
WITHOUT .htaccess file
As you can see the Content-Type is text/css .. like it should be
WITH .htaccess file
As you can see here the mime.type returns it as a text/html
What i've tried.
First. after <title> html tag i've enternet
<base href="/">
Second. I've tried to change the .htaccess file to this
<IfModule mod_rewrite.c>
RewriteEngine on
AddType application/javascript js
AddType text/css css
RewriteCond %{THE_REQUEST} /eventDetails\.php\?id=([0-9]+)&name=(.+)\sHTTP [NC]
RewriteRule ^ /%1/%2? [L,R]
RewriteRule ^([^/]+)/([^/]+)/?$ /eventDetails.php?id=$1&name=$2 [L]
</IfModule>
And lastly i've checked on my dedicated server usr/local/apache/conf/
on the mime.types file that the text/css css is NOT commented the same as
application/javascript js
I dont know what else i should do.
Your second rule
RewriteRule ^([^/]+)/([^/]+)/?$ /eventDetails.php?id=$1&name=$2 [L]
rewrites /css/style.css to /eventDetails.php?id=css&name=style.css.
If you want to exclude existing files from this rule, you may prefix it with
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
You may also change the first rule to
RewriteCond %{QUERY_STRING} id=([0-9]+)&name=(.+)
RewriteRule ^eventDetails\.php$ /%1/%2? [L,R]
and to prevent a redirect loop we insert at the beginning
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
You don't need this of course, if you keep the existing condition and rule against THE_REQUEST.
So the complete rules become
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteCond %{QUERY_STRING} id=([0-9]+)&name=(.+)
RewriteRule ^eventDetails\.php$ /%1/%2? [L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ /eventDetails.php?id=$1&name=$2 [L]

.htacces redirect all traffic to index.php and request for .js to another file

Hi Im Not very comfortable writing htaccess rules, my goal is
Redirect all traffic to index.php
Redirect all request to script.min.js to script.php
ultimately scripts.min.js does not exist rather the script.php will have set headers and include all needed JS files to create one master file. Also the status code should be 200.
Thank you in advance
I haven't tested this, but it'd probably look something like:
RewriteEngine On
RewriteRule ^script.min.js$ script.php [L]
RewriteCond %{REQUEST_URI} !/index.php$ [NC]
RewriteRule (.*) /index.php [L]
I was able to solve the problem by taking wordpress .htaccess and prepending the file match section
<IfModule mod_rewrite.c>
RewriteEngine On
<FilesMatch "\.(js)$">
RewriteRule . /scripts.php [L]
</FilesMatch>
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Trying to apply mod rewrite rule for e.g. css/style.123456.css -> css/style.min.css

I'm trying to create a rewrite rule for any js/css files with a given time stamp to point to the real files but not really sure how to do it. Below is my attempt but when I try this it seems to mess up my
AddDefaultCharset UTF-8
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(js|css)\/(.+)\.(\d+)\.(js|css)$ $1/$2.min.$4
RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
</IfModule>
By the way, my folder structure is as follows:
/public/
/public/index.php
/public/.htaccess (rules are written in this file)
/public/css/style.min.css
/public/js/all.min.js
Also, the apache vhost conf file will direct requests to the /public/ directory. This I can verify works. But when I try to view /css/style.12235543543.css (which I want to point to /css/style.min.css) it just goes through the front controller (index.php) instead of the stylesheet.
Use that:
AddDefaultCharset UTF-8
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^(js|css)/(.+)\.(\d+)\.(js|css)$ $1/$2.min.$4 [L]
RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
</IfModule>

How can I remove "index.php" from my links?

Normally I access a page like this: http://localhost/codeigniter/index.php/auth/login
However I want to be able to access to the page like this : http://localhost/codeigniter/auth/login
I have no information about http.conf and .htaccess configurations.
I have set $config['index_page'] = ''; in codeigniter/application/config/config.php
my http.conf file is at its default except uncommentnig "LoadModule rewrite_module modules/mod_rewrite.so"
I inspected many proposed .htaccess files but I could not understand the logic behind the rewriting rules.
Here is the most successful .htaccess content for me. It is located at /www/CodeIgniter/ .
(by the most successful I mean that this is the only one that gives 404 error created by the CodeIgniter not by the server.)
RewriteOptions Inherit
Options -Indexes
Options +FollowSymLinks
RewriteBase /CodeIgniter/
# Set the default file for indexes
DirectoryIndex index.php
<IfModule mod_rewrite.c>
# activate URL rewriting
RewriteEngine on
# do not rewrite links to the documentation, assets and public files
RewriteCond $1 !^(images|assets|uploads|captcha)
# do not rewrite for php files in the document root, robots.txt or the maintenance page
RewriteCond $1 !^([^\..]+\.php|robots\.txt)
# but rewrite everything else
RewriteRule ^(.*)$ 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.
ErrorDocument 404 index.php
</IfModule>
I hate requesting direct solutions however unfortunately, it is my last resort :/
Can you give me a right solution to my problem?
It's my repeat answer:
Codeigniter issues with paths in localhost (XAMPP)
please create .htaccess file in project folder and write:
<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>
You don't need to define in base_url in config file:
$config['base_url'] = ''; // blank it.
put this on your htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>
make sure the rewrite module is enabled on your server
This is the simplest one i found which works for me :-
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]