HTACCESS: Throw 404 error but still accessible - .htaccess

I have the following structure:
- root
- css
- test.css
- index.php
Is there a way to throw a 404 error when I try to open anything in CSS folder in my browser as:
http://localhost:1993/css
http://localhost:1993/css/test.css
but still accessible from the browser? I want CSS to load normally, but to throw a 404 error when someone opens this path. Or turn off the directory listing for everything in the directory (with all files inside)? Or something like that? Its possible?

You can try mod_access,
<FilesMatch "\.css$">
order deny,allow
deny from all
allow from 127.0.0.1
allow from localhost
</FilesMatch>
With mod_rewrite
RewriteEngine On
RewriteCond %{HTTP_HOST} ^!localhost
RewriteCond %{REQUEST_URI} ^/css [OR]
RewriteCond %{REQUEST_FILENAME} \.css$
RewriteRule ^ - [R=404]

Related

htaccess redirect empty uri instead of directory listing

If I just do Options -Indexes
localhost/subdir/ would give me 500 error.
But I want to redirect it to https://github.com/miranda-zhang/cloud-computing-schema
Something like
RewriteRule ^$ https://github.com/miranda-zhang/cloud-computing-schema [R=308,L]
Currently, this doesn't work on my local server.
I have to do
RewriteRule ^home$ https://github.com/miranda-zhang/cloud-computing-schema [R=308,L]
And use the url localhost/subdir/home
Also Options -Indexes seem to make the following stop working.
ErrorDocument 406 https://miranda-zhang.github.io/cloud-computing-schema/v1.0/406.html
RewriteRule ^.*$ https://miranda-zhang.github.io/cloud-computing-schema/v1.0/406.html [R=406,L]
Full original .htaccess file
I also tried some methods in Problem detecting empty REQUEST_URI with Apache mod_rewrite
The following doesn't seem to work, or maybe I'm missing some other config.
RewriteCond %{HTTP_HOST} ^localhost/coocon
Or
RewriteCond %{REQUEST_URI} "^/$"
Seems something else was wrong, it is a 403 error now.
Forbidden
You don't have permission to access /cocoon/ on this server.
Cannot serve directory /var/www/html/cocoon/: No matching
DirectoryIndex (index.html,index.cgi,index.pl,index.php,index.xhtml,index.htm) found, and server-generated directory index forbidden by Options directive
This might work:
Options -Indexes
ErrorDocument 403 https://github.com/miranda-zhang/cloud-computing-schema

.htaccess direct access to folder forbidden but sibgle image show available

I have an folder /img
I want that isn't allowed to watch alle pictures via /img
But I want to allow img/picture1.jpg
To deny direct access to img folder, you can use
RewriteEngine on
RewriteCond %{THE_REQUEST} /img/.*
RewriteRule ^ - [F]
RewriteRule ^(.+\.gpg|png|gif)$ /img/$1 [L]
This will rewrite /foo.png to /img/foo.png but you will get 403 error if you request /img/foo.png

Directory based error page redirection

I have my website with two localised content namely EN and FR and the content will be rendered through the URLs: http://www.example.com/en/ and http://www.example.com/fr/
I want to implement different error pages for each of these locale but do not I do not have an idea on how to implement through the redirect/virtual host configuration.
I read about different .htaccess file for each of the directory but that did not work.
And help will be good. Thanks in advance.
Having different .htaccess works very well, e.g.
/-+- en -- .htaccess
|
+- fr -- .htaccess
And in /en/.htaccess, you have
ErrorDocument 404 /path/to/en/error404.html
and the same with /fr/.htaccess
ErrorDocument 404 /path/to/fr/error404.html
You can also put error documents in the same directory and differentiate by extension, e.g. error404.en.htaml and error404.fr.html. You have then
ErrorDocument 404 /path/to/error404.en.html
and
ErrorDocument 404 /path/to/error404.fr.html
respectively.
If you don't want to use .htaccess files, you can achieve the same with Directory or Location directives. In your main or virtual host configuration, this would look like
<Directory "/path/to/example.com/en">
ErrorDocument 404 /path/to/error404.en.html
</Directory>
<Directory "/path/to/example.com/fr">
ErrorDocument 404 /path/to/error404.fr.html
</Directory>
The <Location> directive looks similar.
On Apache 2.4 you can use expressions:
# for /fr/ URIs
<If "%{REQUEST_URI} =~ m#^/fr/#">
ErrorDocument 404 /fr-404.html
</If>
# for /en/ URIs
<If "%{REQUEST_URI} =~ m#^/en/#">
ErrorDocument 404 /en-404.html
</If>
Check Apache 2.4 expression reference.
If you're still stuck with Apache 2.2 then you can use:
RewriteEngine On
# for /fr/ URIs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^fr/ /fr-404.html [L]
# for /en/ URIs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^en/ /en-404.html [L]
However keep in mind that Apache 2.2 solution will return 200 status to clients.

Laravel 4.2 project not working after uploading - public folder not visible in browser

I've created a laravel project which is working fine on localhost and my hosting server but when I upload it on my client's server it just doesn't work (he's using bluehost).
When I open the URL which is pointing to /public folder, it should display the homepage. Instead it is showing the listing of all files and folders (except public folder).
When I try to open myurl.com/public or myurl.com/public/index.php in browser, it gives following error:
Forbidden
You don't have permission to access /license/public/ on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
I can see public folder via FTP and I've changed permissions for /app/storage to 777
Here is my public/.htaccess:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
I've checked my /public/error_log, its showing following error:
[21-Aug-2015 09:06:01] PHP Fatal error: require() [<a href='function.require'>function.require</a>]: Failed opening required '__DIR__/../bootstrap/autoload.php' (include_path='.:/usr/lib64/php:/usr/share/pear') in /home6/parentv3/public_html/eldercareapp/license/public/index.php on line 21
Any help?
Well the problem is with your PHP version. If you see your PHP Version, you might get what is wrong with your application. You must use version greater than PHP 5.5
Ok, I had contacted client's hosting company and asked them to upgrade PHP version. They took a couple of days and upgraded PHP to 5.5.
But the problem was still there even after updating PHP. I wasn't able to access /public folder so the problem was somewhere else. After some searching and trying I've found a solution.
The problem was actually in my /public/.htaccess file. I've added just one line in my .htaccess and it solved the problem: Options +FollowSymLinks
My updated .htaccess in /public now looks like:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
Options +FollowSymLinks
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Cheers
Changing permissions to 777 is a terrible idea because it will be vulnerable, instead try with sudo chmod -R 775 project/ where project is the name of your project's folder, then do the next sudo chown -R $USER:$USER project/ to make the project's folder be owned by your user, finally, if you haven't done it, cd into the project's folder and run composer install

Rewrite path before processing directory?

I'm having a small problem with my htaccess files. Currently, it redirects everything back to index.php for processing, except when someone tries to access an actual directory. When the directory exists, it displays the 403 error page instead of rewriting the path to index.php like it's supposed too. What can I modify to make it always go to index.php when the files are accessed via the internet, but still load the correct files in PHP on the server?
Options -Indexes +FollowSymLinks
RewriteEngine On
RewriteBase /
ErrorDocument 403 /index.php?403
ErrorDocument 404 /index.php?404
ErrorDocument 414 /index.php?414
RewriteCond %{HTTP_HOST} !^$ [NC]
RewriteRule !^(.*) index.php [L]
Sample file structure:
Web Directory
- com
- source
- index.php
- TEST.HTML
The folders such as 'com' and source' will display the 403 because the user doesn't have access to them. The files such as 'index.php' and 'TEST.HTML' execute normally. I want my htaccess to redirect everything in the Web Directory folder back to the index.php file, no matter what.
I think you want this instead:
Options -Indexes +FollowSymLinks
RewriteEngine On
RewriteBase /
ErrorDocument 403 /index.php?403
ErrorDocument 404 /index.php?404
ErrorDocument 414 /index.php?414
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule .* index.php [L]
This was on the assumption that you didn't want to be able to access TEST.HTML directly and didn't want to change the URL in the user's browser. If either of those assumptions were wrong, let me know and I'll update the answer with the appropriate rewrite information.

Resources