I have a directory named "includes" containing information about my database and some other important files that users should not access. I want to use an .htaccess file to redirect requests that begin with "/includes" to errors/403.html. This is my code but it does not work. What's the problem?!
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^/includes/([a-zA-Z]+)/?$ /errors/403.html [NC,L]
If your config files are php, you can just place empty index.php in that directory or create .htaccess with Options -Indexes or Deny from All there (in includes/).
Related
I'm trying to use my .htaccess file but I still can't figure it out what am I doing wrong.
I only want my domain to be yescpol.com.br, and it always comes like this
My .htaccess is inside the folder "porcelanato" and it has this code:
RewriteEngine on
RewriteBase /porcelanato/
I only want my index.php to turn into yescpol.com.br
What am I doing wrong?
The easiest thing to do would be to change the DocumentRoot in the domain configuration or move your files out of that folder and into the root.
If you want to stick with the rewrite method, you will need to add a rule like this.
RewriteEngine On
RewriteRule ^$ /porcelanato [L]
Add this line to the top of your .htaccess file:
DirectoryIndex index.php
Though if this property is set properly on your apache service in httpd.conf (or whatever is serving your pages) this won't have to be added to your .htaccess file.
I have the following in a .htaccess file on my server:
<Files index.php>
Order Deny,Allow
Deny from All
Allow from 80.168.22.149
</Files>
The .htaccess is located in a subdirectory, at www.example.com/subdir/.htaccess
The index.php file is located in the same subdirectory.
It gives the usual error if I try and access the page from another IP:
Forbidden
You do not have permission to access this document.
However, when I try and access a subdirectory such as /subdir/images, I get the same error. This error appears if I try and access from an allowed OR disallowed IP address. I thought that the <Files> limitation would only disallow access to the specified files?
I need to be the only IP address that can access index.php, but I need to be able to link to subdirectories within /subdir/ and let anyone view them.
Rather than <Files> directive you can use this rewrite rule in /subdir/.htaccess:
RewriteEngine On
RewriteBase /subdir/
RewriteCond %{REMOTE_HOST} !=80.168.22.149
RewriteRule ^index\.php$ - [F,NC]
The problem with your <Files> directive is that it is matching every index.php including the index.php files in the sub directories.
The only way around this is to use a <Location> directive instead, but you're not able to use that in a .htaccess file.
You would need to edit the Apache configuration file instead and add the <Location> directive in there inside the <VirtualHost> directive for the domain
I am just starting to use .htaccess files because I am on a shared hosting. I tried Google: much about creating a .htaccess file, little about where, how many, do rules cascade, etc.
Here is the map of my site:
/www.mysite.com
|
| .htaccess
| index.php
|
+---includes
| config.php
| functions.php
| some.class.php
| database.class.php
|
\---public
| .htaccess
| index.php
+---css
|
+---js
|
+---images
|
+---admin
index.php
I am putting the rule that redirects the root of the site (www.mysite.com) to the public folder in the .htaccess file that is located in the root.
RewriteEngine On
RewriteBase / public
In the .htaccess file that is located in the public folder, I am putting the rule that removes "/public/" from the URL (www.mysite.com/public/index.php ==> www.mysite.com/index.php).
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule (.*) /public/$1
I also have the following code to restrict access to the .htaccess file itself and to deny ditectory listings along with other (longer) code that denies bots, sets cache options, time zone, etc.
deny from all
Options -Indexes -Multiviews
Do I only need to put that in root .htaccess file (does it trickle down like css)? Does it have to go in the public folder .hyaccess file too?
Do I need .htaccess files in ALL my folders (includes, js, etc.)?
In the includes folder .htaccess file I am putting the code:
<Files config.php>
Order Deny,Allow
Deny from all
</Files>
Is that the correct place for that?
Finally I will password protect the admin/index.php .htpasswd and .htaccess files. This is where I will add/delete users.
Any other recommendations are greatly appreciated.
Thank you in advance.
Your /.htaccess will always be processed, and then down the line to the directory of interest (say, where the script is running) each .htaccess processed in turn. Normally, whatever is set or done in a higher level .htaccess is inherited by lower level .htaccess files (much like CSS). There might be exceptions, but I can't come up with any right now.
When you password protect, it's usually an entire directory and everything beneath it. It might be possible to password protect individual files, but I haven't heard of it. Those files could be protected by access controls in .htaccess.
As a directory inherits its .htaccess-defined settings/protections from above, you only need an .htaccess file in that directory if you want to add or override a setting. For example, you might have Options -Indexes or something, but want visitors to be list the contents of a specific directory (and all children) with Options +Indexes in that directory's .htaccess.
RewriteEngine On
RewriteBase / public
I'm not sure what you're trying to do here. I've only seen RewriteBase with one path.
In the .htaccess file that is located in the public folder, I am putting the rule that
removes "/public/" from the URL (www.mysite.com/public/index.php ==>
www.mysite.com/index.php).
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule (.*) /public/$1
Huh? The real directory includes /public, so you want to rewrite an incoming non-public URI to include public (i.e., add it)? Your code looks fine, if that's your intent.
i am working within sub folder of my website and i want to rewrite my urls.
i have never done this before nor have any knowledge of rewriting rules.
I have a file which named show.php which takes parameter id which is any no and then it echo that no on to the screen.
Now this file is in folder name polo.
www.xyz.com/polo/show.php
i want to rewrite
www.xyz.com/polo/show.php?id=10
to
www.xyz.com/polo/id/10
I tried this answer but still its not working.
Subfolders using htaccess
Any help would be appreciated.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^(polo)/(id)/([0-9]+)/?$ /$1/show.php?$2=$3 [L,QSA,NC]
I need to load single index.html file before actual index.php I have on server.
I have:
index.html - (intro page)
index.php - (actual webpage index file)
.htaccess rewrite rule
Sounds simple, but .htaccess redirects every webpage query back to my index.html file.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.html [QSA,L]
</IfModule>
What is the best way to make it work?
Don't use mod_rewrite, use DirectoryIndex and give priority to index.html
DirectoryIndex index.html index.php
Note: While this should work, I don't recommend it as it creates ambiguity. One of those configuration changes that will haunt you later.
You can simply use DirectoryIndex directive for that:
DirectoryIndex index.html index.php
From the documentation:
The DirectoryIndex directive sets the list of resources to look for, when the client requests an index of the directory by specifying a / at the end of the directory name. Local-url is the (%-encoded) URL of a document on the server relative to the requested directory; it is usually the name of a file in the directory. Several URLs may be given, in which case the server will return the first one that it finds. If none of the resources exist and the Indexes option is set, the server will generate its own listing of the directory.