denying access to sub folder by its real name using htaccess - apache

i want to deny the user to acces the subfolder by its real name :
the real name is "folder1"
i want it to be accessed by an other name "test"
i already done that using this :
RewriteRule ^(test/)(.*)$ /folder1/$2 [L]
it works fine but it still can be accessed by its real name (site.com/folder1)
i already added this after the previous line :
RewriteRule ^(.*)(\/)(.*)$ error.php [L]
wich redirect any the user if he uses anything instead of the "test" but it also let him access it if he uses "folder1".
please some help.

Just turn off indexing on your site.
Add this to the top of you .htaccess file.
Options -Indexes
ErrorDocument 403 /

Related

.htaccess rewriterule is going in a infinity loop

in a domain, I have a .htaccess that is configured like this :
RewriteEngine on
RewriteRule ^admin$ /admin/ [R,L]
RewriteRule (.*) http://www.example.com/sf2/sub/%{REQUEST_URI}?%{QUERY_STRING}&id=7 [P]
in the old server, this is working very well, but in the new server, the rule is going in a loop and it's impossible to come in the website.
When I open the page, I have this error :
Forbidden
You don't have permission to access /sf2/sub/sf2/sub/sf2/sub/......./sf2/sub/sub.php
As you can see, the rule is calling infinity time !!! and I want only one time. I know that this is nothing to do with .htaccess because this is working well in other server that call the same file.
may be this is apache configuration ? I don't know.
If someone has already has this trouble ? How to fixe it ?
I transfer all file from old server (which is working well) to new one (which is not working now)
What I need is : a domain (num 1) is redirected to an other domain (num 2) with the URL of the num 1 and adding a id parameter and after showed the website with the files of num 2 called

Simple url masking via htaccess doesn't work

i'm trying to achieve the following:
I have domain.it and domain.fr
domain.it contains a website localized in multiple languages, so for example if you go to www.domain.it/fr/somecontent.php it shows that content in french language. This works. This website is hosted on a dedicated server.
In domain.fr i have an empty space with only an .htaccess. What i want is that when the user go to www.domain.fr/somecontent.php the browser shows the content from www.domain.it/fr/somecontent.php but keeping www.domain.fr/somecontent.php in the URL. So basically www.domain.fr/* should show the content from www.domain.it/fr/* but keeping www.domain.fr/* in the browser address bar.
Using an iframe is not an option because is not good for the SEO.
I'm using the following code inside the .htaccess on domain.fr (which is hosted on an OVH shared hosting):
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.domain.fr$
RewriteRule ^(.*) https://www.domain.it/fr/$1 [P]
But when i open www.domain.fr or www.domain.fr/somecontent.php in the browser it says "Forbidden: You don't have permission to access /somecontent.php on this server."
Instead, if i place [L] or [L,R=302] in place of the [P] in the last line of .htaccess, it correctly redirect to the www.domain.it/fr/somecontent.php showing it contents, but it shows the destination url (the it domain) in the browser bar.
So i think the rules are correct, but for some reason when i use the [P] flag which as far as i know is needed to mask the url, it doesn't work.
Have you any clues ?
Thank you!

How Remove a folder form url using .htacces

I develop a new site for a client and I don't want to move to the root folder of the server.
Besides that, the hosting service do not alow me to change the physical path.
So I decide to use .htaccess to handle this.
My .htaccess file is like this:
AddHandler php56-script .php
suPHP_ConfigPath /home/balletpaulacastro/
Options +FollowSymLinks
RewriteEngine on
So when the user try to access www.balletpaulacastro.com.br they will be redirected to www.balletpaulacastro.com.br/bpc where the new site is located.
What i want is to rewrite the url to not show de /bpc/ folder.
I try many examples, but i'm not a coder, so if you guys can write down the entire code with my actual url and folders, that will be so nice.
Thanks in advance
Raul
Try :
RewriteEngine On
RewriteRule ^((?!bpc).*)$ /bpc/$1 [NC,L,QSA]
This will rewrite
example.com
to
example.com/bpc

Mod_rewrite based on IP and use linked folder name

I want to use .htaccess to rewrite to a folder based on the visitors IP-address. I have a list of IP-addresses with associated usernames (in a text file). The usernames are also the folder names.
192.168.0.2 - John
192.168.0.3 - Ben
Example: when John (IP ..0.2) goes to www.domain.com, he must see the the content of folder /John
Because the username-IP binding will be made in PHP, I prefer to use RewriteMap so I can generate a .txt file.
Unfortunately, I have to idea where to start. Rewriting based on the users IP is not the problem, the problem is finding the linked username.
Any help appreciated!
There are several ways of doing this, but I wonder why as there are a number of existing modules that provide user directory and authentication already, anyway you could:
# Call an external program each time
RewriteMap userDir "prg:/srv/www/cgi-bin/ipToUserDirectoryMapper.php"
RewriteCond %{REQUEST_URI} !somePatternCommonToAllAlreadyRewrittenRequests
RewriteRule .* /${userDir:%{REMOTE_ADDR}}%{REQUEST_URI} [L,R]
where: ipToUserDirectoryMapper.php takes an IP address as an argument and returns a directory, with a default for unknown IP's.
alternitively, is the mapping is in a txt file:
# Use a static remap file
RewriteMap userDirMap "txt:/srv/www/ipToDirectoryMap.txt"
RewriteCond %{REQUEST_URI} !somePatternCommonToAllAlreadyRewrittenRequests
RewriteRule ^(.*) /${userDirMap:%{REMOTE_ADDR}|someDefaultUser}%{REQUEST_URI} [L,R]
Note: your directory names will need a common element, or you'll need to set a CGI parameter or environment variable for the user directories ( SetEnvIf Request_URI "^/John" alreadyRewritten), to prevent a redirection loop eg. call your directories UserJohn, UserBen and instead of somePatternCommonToAllAlreadyRewrittenRequests have:
**!^/User[a-zA-Z0-9-_]+**

Using .htaccess to restrict access to files

I have 2 domains hosted on the same account but I want to restrict the second one to not access files.
I have abc123.com and zyx987.com.
In my php I do everything I need to display the domain name and template based on the domain but I want only the /download files to be accessed from abc123.com.
The download folder has files like: file1.pdf or file2.zip.
I tried the 'deny from all' but this didnt work since the other domain is blocked too.
Is there another way?
Here's what you have to do. Put this in your .htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?zyx987.com$
RewriteRule ^download - [F]
This will block any request like:
/download/
/download/file1.jpg
/download/another/file.zip
and give a permission denied.