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
1150
votes
32 answers

Access-Control-Allow-Origin Multiple Origin Domains?

Is there a way to allow multiple cross-domains using the Access-Control-Allow-Origin header? I'm aware of the *, but it is too open. I really want to allow just a couple domains. As an example, something like this: Access-Control-Allow-Origin:…
Thomas J Bradley
  • 11,764
  • 3
  • 15
  • 8
508
votes
24 answers

Generic htaccess redirect www to non-www

I would like to redirect www.example.com to example.com. The following htaccess code makes this happen: RewriteCond %{HTTP_HOST} ^www\.example\.com [NC] RewriteRule ^(.*)$ http://example.com/$1 [L,R=301] But, is there a way to do this in a generic…
deepwell
  • 17,897
  • 10
  • 31
  • 37
465
votes
15 answers

How do I manually create a file with a . (dot) prefix in Windows? For example, .htaccess

I want to create a .htaccess file manually and discovered it seems impossible through the Windows UI. I get a "you must type a filename." message. There has to be a way to create files with . as a prefix in Windows. Can this be done manually?
Doug Chamberlain
  • 10,801
  • 9
  • 46
  • 90
332
votes
8 answers

.htaccess: Invalid command 'RewriteEngine', perhaps misspelled or defined by a module not included in the server configuration

I have this error when trying to browse php files locally [Fri Apr 13 19:16:40 2012] [alert] [client 127.0.0.1] C:/AppServ/www/hr-website/.htaccess: Invalid command 'RewriteEngine', perhaps misspelled or defined by a module not included in the…
Adham
  • 58,520
  • 96
  • 215
  • 339
319
votes
14 answers

htaccess redirect to https://www

I have the following htaccess code: RewriteEngine On RewriteCond !{HTTPS} off RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301] RewriteCond %{HTTP_HOST} !^www\. RewriteRule ^(.*)$…
bigben
  • 3,251
  • 4
  • 12
  • 8
311
votes
28 answers

How to redirect all HTTP requests to HTTPS

I'm trying to redirect all insecure HTTP requests on my site (e.g. http://www.example.com) to HTTPS (https://www.example.com). How can I do this in .htaccess file? By the way, I'm using PHP.
Cat
  • 6,270
  • 8
  • 29
  • 34
288
votes
17 answers

Tips for debugging .htaccess rewrite rules

Many posters have problems debugging their RewriteRule and RewriteCond statements within their .htaccess files. Most of these are using a shared hosting service and therefore don't have access to the root server configuration. They cannot avoid…
TerryE
  • 10,316
  • 5
  • 23
  • 46
268
votes
12 answers

How do I disable directory browsing?

I want to disable directory browsing of /galerias folder and all subdirectories Index of /galerias/409 * Parent Directory * i1269372986681.jpg * i1269372986682.jpg * i1269372988680.jpg
arthurprs
  • 3,985
  • 2
  • 23
  • 28
265
votes
37 answers

Laravel 5 – Remove Public from URL

I know this is a very popular question but I haven't been able to find a working solution for Laravel 5. I've been trying to migrate from Codeigniter for a long time, but this convoluted installation process keeps putting me off. I don't want to run…
user1537360
  • 4,385
  • 6
  • 23
  • 22
259
votes
19 answers

.htaccess rewrite to redirect root URL to subdirectory

Trying to get www.example.com to go directly to www.example.com/store I have tried multiple bits of code and none work. What I've tried: Options +FollowSymlinks RewriteEngine on RewriteCond %{HTTP_HOST} ^example.com$ RewriteRule (.*)…
AlphaSmith
245
votes
9 answers

Force SSL/https using .htaccess and mod_rewrite

How can I force to SSL/https using .htaccess and mod_rewrite page specific in PHP.
Sanjay Shah
  • 2,719
  • 2
  • 17
  • 20
239
votes
19 answers

.htaccess redirect all pages to new domain

Which redirect rule would I use to redirect all pages under olddomain.example to be redirected to newdomain.example? The site has a totally different structure, so I want every page under the old domain to be redirected to the new domain index…
Yuval Adam
  • 149,388
  • 85
  • 287
  • 384
235
votes
8 answers

How does RewriteBase work in .htaccess

I have seen this in a few .htaccess examples RewriteBase / It appears to be somewhat similar in functionality to the of HTML. I believe it may automatically prepend its value to the beginning of RewriteRule statements (possibly ones…
alex
  • 438,662
  • 188
  • 837
  • 957
204
votes
13 answers

Redirect non-www to www in .htaccess

I have this in my .htaccess file: RewriteCond %{HTTP_HOST} ^example.com$ RewriteRule (.*) http://www.example.com$1 [R=301,L] but whenever I access a file on my root like http://example.com/robots.txt it will redirect to…
Paul Sanchez
  • 2,231
  • 2
  • 13
  • 9
188
votes
12 answers

How to automatically redirect HTTP to HTTPS on Apache servers?

Environment Centos with apache Trying to setup automatic redirection from http to https From manage.mydomain.com --- To ---> https://manage.mydomain.com I have tried adding the following to my httpd.conf but it didn't work RewriteEngine on …
Deano
  • 9,220
  • 13
  • 51
  • 99
1
2 3
99 100