Questions tagged [.htaccess]

Directory-level configuration file used by Apache web servers. Use this tag if and only if .htaccess content is directly involved in the topic. We know many people are using .htaccess, but kindly ask the members of the community to not use this tag, unless you know it is on-topic in your question. Also notice what the "apache" tag has to say about questions about _configuring_ Apache httpd and their potential off-topicness on stackoverflow

The .htaccess (hypertext access) file is a configuration file for the Apache web server, allowing configuration settings to be made at a directory-specific level. It overrides the server-wide or site-level settings in the main Apache server-level configuration but must be enabled in the server-level configuration to work.

Most (though not all) directory-specific Apache settings can be configured in .htaccess. One of the most common usages for it is URL redirection using mod_rewrite, which is used by many sites for SEO purposes and to provide easier-to-read URLs. The Apache documentation has further information about mod_rewrite as well as simpler solutions for many common use cases.

.htaccess files are also commonly used to define custom error pages (e.g. for 404 errors). They can also be used for specifying directory-specific security and user authentication, configuring the PHP installation, file handlers and MIME types, output compression, and more.

For more information on .htaccess files and how to use them, see the Apache documentation.

More information

Top Questions for reference

  1. Tips for debugging .htaccess rewrite rules
  2. Access-Control-Allow-Origin Multiple Origin Domains?
  3. How to manually create a file with a . dot prefix in windows for example .htaccess
  4. How does RewriteBase work in .htaccess
  5. htaccess redirect to https://www
  6. How to redirect all HTTP requests to HTTPS
  7. Hidden features of mod_rewrite
  8. .htaccess redirect all pages to new domain
  9. Create subdomains on the fly with .htaccess (PHP)
  10. .htaccess mod_rewrite - how to exclude directory from rewrite rule
  11. Leverage browser caching for 3rd party JS

How to ask

  • Make sure you explain how .htaccess is involved in your question
  • Explain the expected behavior, the actual behavior and the difference between the two
  • Show the relevant part of the .htaccess
70056 questions
136
votes
8 answers

deny direct access to a folder and file by htaccess

Here is the scenario: There is a index.php file in root folder some files are included in index.php which are in the includes folder. 1 other file (submit.php) is in the root folder for form submit action. I want to restrict direct user access to…
Imrul.H
  • 5,430
  • 13
  • 48
  • 82
136
votes
9 answers

Make .git directory web inaccessible

I have a website that I use github (closed source) to track changes and update site. The only problem is, it appears the .git directory is accessible via the web. How can I stop this and still be able to use git? Should I use .htaccess? Should I…
Chris Muench
  • 15,874
  • 66
  • 193
  • 332
124
votes
9 answers

Create subdomains on the fly with .htaccess (PHP)

I am looking to create a system which on signup will create a subdomain on my website for the users account area. e.g. johndoe.website.com I think it would be something to do with the .htaccess file and possibly redirecting to another location on…
Ben McRae
  • 3,411
  • 11
  • 34
  • 30
119
votes
8 answers

Hidden features of mod_rewrite

There seem to be a decent number of mod_rewrite threads floating around lately with a bit of confusion over how certain aspects of it work. As a result I've compiled a few notes on common functionality, and perhaps a few annoying nuances. What other…
Owen
  • 76,727
  • 20
  • 113
  • 113
118
votes
8 answers

How to debug .htaccess RewriteRule not working

I have a RewriteRule in a .htaccess file that isn't doing anything. How do I troubleshoot this? How can I verify if the .htaccess file is even being read and obeyed by Apache? Can I write an echo "it is working" message, if I do write it, where…
macha
  • 6,657
  • 17
  • 56
  • 82
118
votes
5 answers

How to deny access to a file in .htaccess

I have the following .htaccess file: RewriteEngine On RewriteBase / # Protect the htaccess file Order Allow,Deny Deny from all # Protect log.txt Order Allow,Deny Deny from all #…
Timothée HENRY
  • 12,701
  • 16
  • 82
  • 132
116
votes
5 answers

Enabling error display in PHP via htaccess only

I am testing a website online. Right now, the errors are not being displayed (but I know they exist). I have access to only the .htaccess file. How do I make all errors to display using my .htaccess file? I added these lines to my .htaccess…
Ogugua Belonwu
  • 2,051
  • 6
  • 26
  • 44
114
votes
9 answers

deny directory listing with htaccess

I have a folder, for example : /public_html/Davood/ and too many sub folder in folder, for example : /public_html/Davood/Test1/ , /public_html/Davood/Test1/Test/ , /public_html/Davood/Test2/ , ... I want add a htaccess file into /public_html/Davood/…
DJafari
  • 9,798
  • 8
  • 36
  • 56
110
votes
17 answers

How to check whether mod_rewrite is enable on server?

Currently I am using the hosting with lightspeed server. Hosting says mod_rewrite is enabled but I can't get my script working there. Whenever I try to access the URL, it returns 404 - not found page. I put the same codes at another server which is…
knightrider
  • 2,253
  • 7
  • 28
  • 42
110
votes
15 answers

Remove .php extension with .htaccess

Yes, I've read the Apache manual and searched here. For some reason I simply cannot get this to work. The closest I've come is having it remove the extension, but it points back to the root directory. I want this to just work in the directory that…
Dirty Bird Design
  • 4,671
  • 12
  • 56
  • 115
106
votes
9 answers

How to use a RELATIVE path with AuthUserFile in htaccess?

I have a .htaccess that uses basic authentication. It seems the path to the .htpasswd file isn't relative to the htaccess file, but instead to the server config. So even though I have the .htaccess and .htpasswd files in the same directory, this…
DssTrainer
  • 1,369
  • 3
  • 11
  • 15
106
votes
8 answers

.htaccess - how to force "www." in a generic way?

This will change domain.com to www.domain.com: # Force the "www." RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC] RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L] How do I replace the "domain" part so that this works on any domain?
StackOverflowNewbie
  • 35,023
  • 98
  • 252
  • 421
101
votes
7 answers

Do you have to restart apache to make re-write rules in the .htaccess take effect?

I have pushed my .htaccess files to the production severs, but they don't work. Would a restart be the next step, or should I check something else.
Jesse Hattabaugh
  • 7,359
  • 8
  • 30
  • 35
101
votes
3 answers

What does $1 [QSA,L] mean in my .htaccess file?

I need to change my .htaccess and there are two lines which I don't understand. RewriteCond %{REQUEST_FILENAME} !-l RewriteRule ^(.+)$ index.php?url=$1 [QSA,L] When I should use these lines ?
yossi
  • 1,351
  • 2
  • 10
  • 15
100
votes
14 answers

.htaccess redirect http to https

I have an old url (www1.test.net) and I would like to redirect it to https://www1.test.net I have implemented and installed our SSL certificate on my site. This is my old file .htaccess: RewriteEngine On RewriteRule !\.(js|gif|jpg|png|css|txt)$…
Bàn Chân Trần
  • 1,342
  • 2
  • 16
  • 24