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 [tag:apache] tag has to say about questions about _configuring_ Apache httpd and their potential off-topicness on Stack Overflow

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 do I 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 using .htaccess rules?
  7. Hidden features of mod_rewrite
  8. .htaccess redirect all pages to the homepage on a 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
72915 questions
201
votes
11 answers

How to Set AllowOverride all

I want to set the AllowOverride all But I don't know how to do it. I have found the following code by searching the google and pasted it in .htaccess: AllowOverride All But after pasting it I started receiving…
Munib
  • 3,533
  • 9
  • 29
  • 37
188
votes
11 answers

Deny access to one specific folder in .htaccess

I'm trying to deny users from accessing the site/includes folder by manipulating the URL. I don't know if I have to deny everything and manually make individual exceptions to allow, if I can just deny this one folder, or if there's a rewrite…
user2835191
187
votes
9 answers

htaccess Access-Control-Allow-Origin

I'm creating a script that loads externally on other sites. It loads CSS and HTML and works fine on my own servers. However, when I try it on another website it displays this awful error: Access-Control-Allow-Origin Here you can see it loads…
Tzook Bar Noy
  • 11,337
  • 14
  • 51
  • 82
182
votes
19 answers

Cached, PHP generated Thumbnails load slowly

Question Part A ▉ (100 bountys, awarded) Main question was how to make this site, load faster. First we needed to read these waterfalls. Thanks all for your suggestions on the waterfall readout analysis. Evident from the various waterfall graphs…
Sam
  • 15,254
  • 25
  • 90
  • 145
177
votes
18 answers

How to remove .html from URL?

How to remove .html from the URL of a static page? Also, I need to redirect any url with .html to the one without it. (i.e. www.example.com/page.html to www.example.com/page ).
Dave
  • 3,328
  • 7
  • 26
  • 30
176
votes
35 answers

CodeIgniter removing index.php from url

My current urls look like this [mysite]index.php/[rest of the slug]. I want to strip index.php from these urls. mod_rewrite is enabled on my apache2 server. In config, $config['index_page'] = ''; My codeignitor root .htaccess file contains,…
Nihar Sarangi
  • 4,845
  • 8
  • 27
  • 32
175
votes
8 answers

Redirect all to index.php using htaccess

I am writing a simple PHP-based MVC-ish framework. I want this framework to be able to be installed in any directory. My PHP script grabs the request uri and breaks it off into segments. It makes segment 1 the controller and segment 2 the action.…
David Ericsson
  • 2,570
  • 2
  • 19
  • 33
174
votes
13 answers

Deny all, allow only one IP through htaccess

I'm trying to deny all and allow only for a single IP. But, I would like to have the following htaccess working for that single IP. I'm not finding a way to have both working: the deny all and allow only one, plus the following options:
punkbit
  • 7,347
  • 10
  • 55
  • 89
161
votes
5 answers

Reference: mod_rewrite, URL rewriting and "pretty links" explained

"Pretty links" is an often requested topic, but it is rarely fully explained. mod_rewrite is one way to make "pretty links", but it's complex and its syntax is very terse, hard to grok, and the documentation assumes a certain level of proficiency in…
deceze
  • 510,633
  • 85
  • 743
  • 889
160
votes
6 answers

.htaccess mod_rewrite - how to exclude directory from rewrite rule

I have 8 lines of rewrite rules in my .htaccess file. I need to exclude two physical directories on my server from these rules, so they can become accessible. For now all requests are sent to index.php file. Directories to exclude: "admin" and…
Kelvin
  • 8,813
  • 11
  • 38
  • 36
158
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
  • 17,444
  • 70
  • 209
  • 362
157
votes
19 answers

HTTP authentication logout via PHP

What is the correct way to log out of HTTP authentication protected folder? There are workarounds that can achieve this, but they are potentially dangerous because they can be buggy or don't work in certain situations / browsers. That is why I am…
Josef Sábl
  • 7,538
  • 9
  • 54
  • 66
155
votes
5 answers

URL rewriting with PHP

I have a URL that looks like: url.com/picture.php?id=51 How would I go about converting that URL to: picture.php/Some-text-goes-here/51 I think WordPress does the same. How do I go about making friendly URLs in PHP?
Jazerix
  • 4,729
  • 10
  • 39
  • 71
155
votes
13 answers

.htaccess ErrorDocument 404 not showing up

I have a server from AWS EC2 service running on Linux ubuntu and I have installed apache, php, and mysql. I have added a .htaccess file in my document root /var/www/html. I entered this code in it: ErrorDocument 404 /var/www/html/404.php and it is…
Shalin Shah
  • 8,145
  • 6
  • 31
  • 44
154
votes
5 answers

How to prevent http file caching in Apache httpd (MAMP)

I am developing a single page Javascript application in MAMP. My JavaScript and HTML template files are getting cached between requests. Is there a simple way to indicate in MAMP that I want to prevent http file caching? Possibly with a .htaccess…
dmck
  • 7,801
  • 7
  • 43
  • 79