.htaccess force 404 and prevent directory listing - .htaccess

Is it possible to force a 404 error if someone tries to access any directory in my root directory (/var/www) provided the directory name contains .git?
I.e if someone tried to access http://mysite.com/site.git, it would show a 404 page?
Furthermore, how could I prevent directory listing in the same file?
I am using Apache/2.2.22 (Ubuntu).
Thanks,
Max.

Using mod_rewrite, you can put these rules in the htaccess file in your document root:
RewriteEngine On
RewriteRule \.git$ - [R=404,L]
Or, you can replace the brackets with a [F,L] to return a 403 Forbidden instead.

Related

Redirect everything to 404 except files in public folder

How redirect everything inside rootfolder to 404, except files in root/public folder? Directory listing must be disabled everywhere and .htaccessredirected to 404 as well. It's possible to make that with one .htaccess file?
Currently i have Redirect 404 / in root, but when trying to access .htaccess, it's forbidden. Thanks
To redirect everything inside rootfolder to 404, except files in root/public folder ,you can use the following Rule in your /root/.htaccess .
RewriteEngine on
RewriteRule !public - [R=404,L]
and to disable directory listing for all folder/subfolders, add this line to your /root/.htaccess
Options -Indexes

Exclude certain directory from RewriteEngine on a Server running TYPO3

I want to develop a new website for a customer who is currently running a TYPO3 website on his server.
I'd like to exclude a subdirectory from the TYPO3 RewriteEngine (realurl?) so I have normal access to that subdirectory.
www.domain.com/dev
for example.
I have access to that site but as soon as I put a .htaccess file in that certain directory to protect it with a htpasswd. I get a TYPO3 Error:
Reason: Segment "dev" was not a keyword for a postVarSet as expected!
In .htaccess find this
# Stop rewrite processing, if we are in the typo3/ directory or any other known directory
# NOTE: Add your additional local storages here
RewriteRule (?:typo3/|fileadmin/|typo3conf/|typo3temp/|uploads/|favicon\.ico) - [L]
and add your directory there
# Stop rewrite processing, if we are in the typo3/ directory or any other known directory
# NOTE: Add your additional local storages here
RewriteRule (?:typo3/|fileadmin/|typo3conf/|typo3temp/|uploads/|favicon\.ico|dev/) - [L]
I found the solution and it was anything but straightforward.
I needed to add this line to my .htaccess in the protected subdirectory:
ErrorDocument 401 "Not authorized"
Apparently there is a conflict between URL rewriting and Basic Auths in cases just like mine. The Server writes a "401 Unauthorized" Header and looks for an existing error document based on a pre-defined path which then gets rewritten.

Rewrite rule to redirect to file and not folder with same name

I am working on a new website and I want the following url /newsite/products to redirect to /newsite/products/product.php.
My .htaccess file is as follows:
RewriteEngine On
RewriteRule ^newsite/products$ /newsite/products/product.php
My problem is that there is already a folder called products within newsite and when requesting newsite/products it tries to open that folder rather than go to the php file.
I get:
Forbidden
You don't have permission to access /newsite/products/ on this server.
Is there any way to make this work?
EDIT: Requested url is: http://example.com/newsite/products and location of .htaccess is in root.
You need to turn off the directory slash to rewrite your file :
Try :
DirectorySlash off
RewriteEngine On
RewriteRule ^newsite/products$ /newsite/products/product.php [NC,L]
Don't add a trailing slash in rule pattern
/newsite/products
will rewrite to
/newsite/products/product.php
and
/newsite/products/
will open your "products" directory
Note- this is not a safe solution, if you remove the rule and DirectorySlash is off, Your index file will be ignored ,all directories will start listing files and folders.

set htaccess for access to this file Directly

i want all of file in folder /client/ just available to access by subdomain.site.com
to another say i have Some swf(flash) file in folder /client/ For example /client/1.swf that if user enter this address in browser: site.com/client/1.swf
he can's access to this file Directly
but i want user just can accessible to site.com/client/1.swf address through subdomain.site.com
anybody can help me for set htaccess file !?
Try adding this to the htaccess file in your document root:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^subdomain\.site\.com$ [NC]
RewriteRule ^client/ - [L,F]
If you would rather return a 404 instead of a 403 (forbidden) then change the stuff in the square brackets to: [L,R=404]
Another way is to Create an .htaccess file in client. Like /client/.htaccess Then put this code.
Apache 2.2
order deny,allow
deny from all
allow from 127.0.0.1
Apache 2.4
Require all denied
Require ip 127.0.0.1

Can't access to my Magmi subfolder in my Magento folder

I useing Magmi to import/export products, but when I visit my Magmi subfolder (www.mysite.com/magmi/web/magmi.php) is allways redirect me to my homepage. If I rename - delete - move - turn off the magento main folder .htaccess file I can access to my Magmi subfolder. Can somebody help me please how I can visit the Magmi subfolder without turning of the main .htaccess file?
Inside your Magento's .htaccess just after RewriteBase line add this rule:
RewriteRule ^magmi/ - [L,NC]
This will skip Magento's rewrite handling for /magmi/ URIs.
Add this to your .htaccess file in the magmi folder:
ErrorDocument 401 "Unauthorized Access"
This way, the browser will ask for a user/password, instead of 404'ing.
(See https://serverfault.com/questions/55323/disable-mod-rewrite-for-subdirectory)
in my case, the magmi folder had the wrong permissions. Try setting 755 permission on magmi folder and subfolders

Resources