{"id":225,"url":"https://github.com/phanan/htaccess","last_synced_at":"2025-09-27T09:31:06.504Z","repository":{"id":26449590,"uuid":"29900673","full_name":"phanan/htaccess","owner":"phanan","description":"✂A collection of useful .htaccess snippets.","archived":false,"fork":false,"pushed_at":"2023-03-22T03:43:06.000Z","size":211,"stargazers_count":12616,"open_issues_count":10,"forks_count":1325,"subscribers_count":463,"default_branch":"master","last_synced_at":"2024-05-20T04:34:47.802Z","etag":null,"topics":["apache","awesome","awesome-list","dev-ops","htaccess","htaccess-snippets","list","snippets"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/phanan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null},"funding":{"github":["phanan"],"open_collective":"koel"}},"created_at":"2015-01-27T06:25:41.000Z","updated_at":"2024-05-18T19:43:55.000Z","dependencies_parsed_at":"2022-07-17T09:46:33.509Z","dependency_job_id":"b2fca69a-cb6a-4f8b-a3eb-c48374c1540f","html_url":"https://github.com/phanan/htaccess","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phanan%2Fhtaccess","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phanan%2Fhtaccess/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phanan%2Fhtaccess/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phanan%2Fhtaccess/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/phanan","download_url":"https://codeload.github.com/phanan/htaccess/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":234418149,"owners_count":18829580,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["apache","awesome","awesome-list","dev-ops","htaccess","htaccess-snippets","list","snippets"],"created_at":"2024-01-05T20:12:49.598Z","updated_at":"2025-09-27T09:31:06.418Z","avatar_url":"https://github.com/phanan.png","language":null,"readme":"# .htaccess Snippets [![Awesome](https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg)](https://github.com/sindresorhus/awesome)\nA collection of useful .htaccess snippets, all in one place.\n\n**NOTE**: `.htaccess` files are for people that do not have rights to edit the main server configuration file. They are intrinsically slower and more complicated than using the main config. Please see the [howto in the httpd documentation](https://httpd.apache.org/docs/current/howto/htaccess.html) for further details.\n\n**Disclaimer**: While dropping the snippet into an `.htaccess` file is most of the time sufficient, there are cases when certain modifications might be required. Use at your own risk.\n\n**IMPORTANT**: Apache 2.4 introduces a few breaking changes, most notably in access control configuration. For more information, check the [upgrading document](https://httpd.apache.org/docs/2.4/upgrading.html) as well as [this issue](https://github.com/phanan/htaccess/issues/2).\n\n## Credits\nWhat we are doing here is mostly collecting useful snippets from all over the interwebs (for example, a good chunk is from [Apache Server Configs](https://github.com/h5bp/server-configs-apache)) into one place. While we’ve been trying to credit where due, things might be missing. If you believe anything here is your work and credits should be given, let us know, or just send a PR.\n\n## Table of Contents\n- [Rewrite and Redirection](#rewrite-and-redirection)\n    - [Force www](#force-www)\n    - [Force www in a Generic Way](#force-www-in-a-generic-way)\n    - [Force non-www](#force-non-www)\n    - [Force non-www in a Generic Way](#force-non-www-in-a-generic-way)\n    - [Force HTTPS](#force-https)\n    - [Force HTTPS Behind a Proxy](#force-https-behind-a-proxy)\n    - [Force Trailing Slash](#force-trailing-slash)\n    - [Remove Trailing Slash](#remove-trailing-slash)\n    - [Redirect a Single Page](#redirect-a-single-page)\n    - [Redirect Using RedirectMatch](#redirect-using-redirectmatch)\n    - [Alias a Single Directory](#alias-a-single-directory)\n    - [Alias Paths to Script](#alias-paths-to-script)\n    - [Redirect an Entire Site](#redirect-an-entire-site)\n    - [Alias \"Clean\" URLs](#alias-clean-urls)\n    - [Exclude a URL from Redirection](#exclude-url-from-redirection)\n- [Security](#security)\n    - [Deny All Access](#deny-all-access)\n    - [Deny All Access Except Yours](#deny-all-access-except-yours)\n    - [Allow All Access Except Spammers'](#allow-all-access-except-spammers)\n    - [Deny Access to Hidden Files and Directories](#deny-access-to-hidden-files-and-directories)\n    - [Deny Access to Backup and Source Files](#deny-access-to-backup-and-source-files)\n    - [Disable Directory Browsing](#disable-directory-browsing)\n    - [Disable Image Hotlinking](#disable-image-hotlinking)\n    - [Disable Image Hotlinking for Specific Domains](#disable-image-hotlinking-for-specific-domains)\n    - [Password Protect a Directory](#password-protect-a-directory)\n    - [Password Protect a File or Several Files](#password-protect-a-file-or-several-files)\n    - [Block Visitors by Referrer](#block-visitors-by-referrer)\n    - [Prevent Framing the Site](#prevent-framing-the-site)\n- [Performance](#performance)\n    - [Compress Text Files](#compress-text-files)\n    - [Set Expires Headers](#set-expires-headers)\n    - [Turn eTags Off](#turn-etags-off)\n- [Miscellaneous](#miscellaneous)\n    - [Set PHP Variables](#set-php-variables)\n    - [Custom Error Pages](#custom-error-pages)\n    - [Force Downloading](#force-downloading)\n    - [Prevent Downloading](#prevent-downloading)\n    - [Allow Cross-Domain Fonts](#allow-cross-domain-fonts)\n    - [Auto UTF-8 Encode](#auto-utf-8-encode)\n    - [Switch to Another PHP Version](#switch-to-another-php-version)\n    - [Disable Internet Explorer Compatibility View](#disable-internet-explorer-compatibility-view)\n    - [Serve WebP Images](#serve-webp-images)\n\n## Rewrite and Redirection\nNote: It is assumed that you have `mod_rewrite` installed and enabled.\n\n### Force www\n``` apacheconf\nRewriteEngine on\nRewriteCond %{HTTP_HOST} ^example\\.com [NC]\nRewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC]\n```\n\n### Force www in a Generic Way\n``` apacheconf\nRewriteCond %{HTTP_HOST} !^$\nRewriteCond %{HTTP_HOST} !^www\\. [NC]\nRewriteCond %{HTTPS}s ^on(s)|\nRewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]\n```\nThis works for _any_ domain. [Source](https://stackoverflow.com/questions/4916222/htaccess-how-to-force-www-in-a-generic-way)\n\n### Force non-www\nIt’s [still](http://www.sitepoint.com/domain-www-or-no-www/) [open](https://devcenter.heroku.com/articles/apex-domains) [for](http://yes-www.org/) [debate](http://no-www.org/) whether www or non-www is the way to go, so if you happen to be a fan of bare domains, here you go:\n``` apacheconf\nRewriteEngine on\nRewriteCond %{HTTP_HOST} ^www\\.example\\.com [NC]\nRewriteRule ^(.*)$ http://example.com/$1 [L,R=301]\n```\n\n### Force non-www in a Generic Way\n``` apacheconf\nRewriteEngine on\nRewriteCond %{HTTP_HOST} ^www\\.\nRewriteCond %{HTTPS}s ^on(s)|off\nRewriteCond http%1://%{HTTP_HOST} ^(https?://)(www\\.)?(.+)$\nRewriteRule ^ %1%3%{REQUEST_URI} [R=301,L]\n```\n\n### Force HTTPS\n``` apacheconf\nRewriteEngine on\nRewriteCond %{HTTPS} !on\nRewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}\n\n# Note: It’s also recommended to enable HTTP Strict Transport Security (HSTS)\n# on your HTTPS website to help prevent man-in-the-middle attacks.\n# See https://developer.mozilla.org/en-US/docs/Web/Security/HTTP_strict_transport_security\n\u003cIfModule mod_headers.c\u003e\n    # Remove \"includeSubDomains\" if you don't want to enforce HSTS on all subdomains\n    Header always set Strict-Transport-Security \"max-age=31536000;includeSubDomains\"\n\u003c/IfModule\u003e\n```\n\n### Force HTTPS Behind a Proxy\nUseful if you have a proxy in front of your server performing TLS termination.\n``` apacheconf\nRewriteCond %{HTTP:X-Forwarded-Proto} !https\nRewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}\n```\n\n### Force Trailing Slash\n``` apacheconf\nRewriteCond %{REQUEST_URI} /+[^\\.]+$\nRewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]\n```\n\n### Remove Trailing Slash\nThis snippet will redirect paths ending in slashes to their non-slash-terminated counterparts (except for actual directories), e.g. `http://www.example.com/blog/` to `http://www.example.com/blog`. This is important for SEO, since it’s [recommended](http://overit.com/blog/canonical-urls) to have a canonical URL for every page.\n``` apacheconf\nRewriteCond %{REQUEST_FILENAME} !-d\nRewriteCond %{REQUEST_URI} (.+)/$\nRewriteRule ^ %1 [R=301,L]\n```\n[Source](https://stackoverflow.com/questions/21417263/htaccess-add-remove-trailing-slash-from-url#27264788)\n\n### Redirect a Single Page\n``` apacheconf\nRedirect 301 /oldpage.html http://www.example.com/newpage.html\nRedirect 301 /oldpage2.html http://www.example.com/folder/\n```\n[Source](http://css-tricks.com/snippets/htaccess/301-redirects/)\n\n### Redirect Using RedirectMatch\n``` apacheconf\nRedirectMatch 301 /subdirectory(.*) http://www.newsite.com/newfolder/$1\nRedirectMatch 301 ^/(.*).htm$ /$1.html\nRedirectMatch 301 ^/200([0-9])/([^01])(.*)$ /$2$3\nRedirectMatch 301 ^/category/(.*)$ /$1\nRedirectMatch 301 ^/(.*)/htaccesselite-ultimate-htaccess-article.html(.*) /htaccess/htaccess.html\nRedirectMatch 301 ^/(.*).html/1/(.*) /$1.html$2\nRedirectMatch 301 ^/manual/(.*)$ http://www.php.net/manual/$1\nRedirectMatch 301 ^/dreamweaver/(.*)$ /tools/$1\nRedirectMatch 301 ^/z/(.*)$ http://static.askapache.com/$1\n```\n[Source](http://www.askapache.com/htaccess/301-redirect-with-mod_rewrite-or-redirectmatch.html#301_Redirects_RedirectMatch)\n\n### Alias a Single Directory\n``` apacheconf\nRewriteEngine On\nRewriteRule ^source-directory/(.*) /target-directory/$1 [R=301,L]\n```\n\n### Alias Paths to Script\n``` apacheconf\nFallbackResource /index.fcgi\n```\nThis example has an `index.fcgi` file in some directory, and any requests within that directory that fail to resolve a filename/directory will be sent to the `index.fcgi` script. It’s good if you want `baz.foo/some/cool/path` to be handled by `baz.foo/index.fcgi` (which also supports requests to `baz.foo`) while maintaining `baz.foo/css/style.css` and the like. Get access to the original path from the PATH_INFO environment variable, as exposed to your scripting environment.\n\n``` apacheconf\nRewriteEngine On\nRewriteRule ^$ index.fcgi/ [QSA,L]\nRewriteCond %{REQUEST_FILENAME} !-f\nRewriteCond %{REQUEST_FILENAME} !-d\nRewriteRule ^(.*)$ index.fcgi/$1 [QSA,L]\n```\nThis is a less efficient version of the FallbackResource directive (because using `mod_rewrite` is more complex than just handling the `FallbackResource` directive), but it’s also more flexible.\n\n### Redirect an Entire Site\n``` apacheconf\nRedirect 301 / http://newsite.com/\n```\nThis way does it with links intact. That is `www.oldsite.com/some/crazy/link.html` will become `www.newsite.com/some/crazy/link.html`. This is extremely helpful when you are just “moving” a site to a new domain. [Source](http://css-tricks.com/snippets/htaccess/301-redirects/)\n\n### Alias “Clean” URLs\nThis snippet lets you use “clean” URLs -- those without a PHP extension, e.g. `example.com/users` instead of `example.com/users.php`.\n``` apacheconf\nRewriteEngine On\nRewriteCond %{SCRIPT_FILENAME} !-d\nRewriteRule ^([^.]+)$ $1.php [NC,L]\n```\n[Source](http://www.abeautifulsite.net/access-pages-without-the-php-extension-using-htaccess/)\n\n### Exclude URL from Redirection\nThis snippet allows you to exclude a URL from redirection.  For example, if you have redirection rules setup but want to exclude robots.txt so search engines can access that URL as expected.\n``` apacheconf\nRewriteEngine On\nRewriteRule ^robots.txt - [L]\n```\n\n## Security\n### Deny All Access\n``` apacheconf\n## Apache 2.2\nDeny from all\n\n## Apache 2.4\n# Require all denied\n```\n\nBut wait, this will lock you out from your content as well! Thus introducing...\n\n### Deny All Access Except Yours\n``` apacheconf\n## Apache 2.2\nOrder deny,allow\nDeny from all\nAllow from xxx.xxx.xxx.xxx\n\n## Apache 2.4\n# Require all denied\n# Require ip xxx.xxx.xxx.xxx\n```\n`xxx.xxx.xxx.xxx` is your IP. If you replace the last three digits with `0/12` for example, this will specify a range of IPs within the same network, thus saving you the trouble to list all allowed IPs separately. [Source](http://speckyboy.com/2013/01/08/useful-htaccess-snippets-and-hacks/)\n\nNow of course there's a reversed version:\n\n### Allow All Access Except Spammers'\n``` apacheconf\n## Apache 2.2\nOrder deny,allow\nDeny from xxx.xxx.xxx.xxx\nDeny from xxx.xxx.xxx.xxy\n\n## Apache 2.4\n# Require all granted\n# Require not ip xxx.xxx.xxx.xxx\n# Require not ip xxx.xxx.xxx.xxy\n```\n\n### Deny Access to Hidden Files and Directories\nHidden files and directories (those whose names start with a dot `.`) should most, if not all, of the time be secured. For example: `.htaccess`, `.htpasswd`, `.git`, `.hg`...\n``` apacheconf\nRewriteCond %{SCRIPT_FILENAME} -d [OR]\nRewriteCond %{SCRIPT_FILENAME} -f\nRewriteRule \"(^|/)\\.\" - [F]\n```\n\nAlternatively, you can just raise a “Not Found” error, giving the attacker no clue:\n``` apacheconf\nRedirectMatch 404 /\\..*$\n```\n\n### Deny Access to Backup and Source Files\nThese files may be left by some text/HTML editors (like Vi/Vim) and pose a great security danger if exposed to public.\n``` apacheconf\n\u003cFilesMatch \"(\\.(bak|config|dist|fla|inc|ini|log|psd|sh|sql|swp)|~)$\"\u003e\n    ## Apache 2.2\n    Order allow,deny\n    Deny from all\n    Satisfy All\n\n    ## Apache 2.4\n    # Require all denied\n\u003c/FilesMatch\u003e\n```\n[Source](https://github.com/h5bp/server-configs-apache)\n\n### Disable Directory Browsing\n``` apacheconf\nOptions All -Indexes\n```\n\n### Disable Image Hotlinking\n``` apacheconf\nRewriteEngine on\n# Remove the following line if you want to block blank referrer too\nRewriteCond %{HTTP_REFERER} !^$\n\nRewriteCond %{HTTP_REFERER} !^https?://(.+\\.)?example.com [NC]\nRewriteRule \\.(jpe?g|png|gif|bmp)$ - [NC,F,L]\n\n# If you want to display a “blocked” banner in place of the hotlinked image,\n# replace the above rule with:\n# RewriteRule \\.(jpe?g|png|gif|bmp) http://example.com/blocked.png [R,L]\n```\n\n### Disable Image Hotlinking for Specific Domains\nSometimes you want to disable image hotlinking from some bad guys only.\n``` apacheconf\nRewriteEngine on\nRewriteCond %{HTTP_REFERER} ^https?://(.+\\.)?badsite\\.com [NC,OR]\nRewriteCond %{HTTP_REFERER} ^https?://(.+\\.)?badsite2\\.com [NC,OR]\nRewriteRule \\.(jpe?g|png|gif|bmp)$ - [NC,F,L]\n\n# If you want to display a “blocked” banner in place of the hotlinked image,\n# replace the above rule with:\n# RewriteRule \\.(jpe?g|png|gif|bmp) http://example.com/blocked.png [R,L]\n```\n\n### Password Protect a Directory\nFirst you need to create a `.htpasswd` file somewhere in the system:\n``` bash\nhtpasswd -c /home/fellowship/.htpasswd boromir\n```\n\nThen you can use it for authentication:\n``` apacheconf\nAuthType Basic\nAuthName \"One does not simply\"\nAuthUserFile /home/fellowship/.htpasswd\nRequire valid-user\n```\n\n### Password Protect a File or Several Files\n``` apacheconf\nAuthName \"One still does not simply\"\nAuthType Basic\nAuthUserFile /home/fellowship/.htpasswd\n\n\u003cFiles \"one-ring.o\"\u003e\nRequire valid-user\n\u003c/Files\u003e\n\n\u003cFilesMatch ^((one|two|three)-rings?\\.o)$\u003e\nRequire valid-user\n\u003c/FilesMatch\u003e\n```\n\n### Block Visitors by Referrer\nThis denies access for all users who are coming from (referred by) a specific domain.\n[Source](http://www.htaccess-guide.com/deny-visitors-by-referrer/)\n``` apacheconf\nRewriteEngine on\n# Options +FollowSymlinks\nRewriteCond %{HTTP_REFERER} somedomain\\.com [NC,OR]\nRewriteCond %{HTTP_REFERER} anotherdomain\\.com\nRewriteRule .* - [F]\n```\n\n### Prevent Framing the Site\nThis prevents the website to be framed (i.e. put into an `iframe` tag), when still allows framing for a specific URI.\n``` apacheconf\nSetEnvIf Request_URI \"/starry-night\" allow_framing=true\nHeader set X-Frame-Options SAMEORIGIN env=!allow_framing\n```\n\n## Performance\n### Compress Text Files\n``` apacheconf\n\u003cIfModule mod_deflate.c\u003e\n\n    # Force compression for mangled headers.\n    # https://developer.yahoo.com/blogs/ydn/pushing-beyond-gzipping-25601.html\n    \u003cIfModule mod_setenvif.c\u003e\n        \u003cIfModule mod_headers.c\u003e\n            SetEnvIfNoCase ^(Accept-EncodXng|X-cept-Encoding|X{15}|~{15}|-{15})$ ^((gzip|deflate)\\s*,?\\s*)+|[X~-]{4,13}$ HAVE_Accept-Encoding\n            RequestHeader append Accept-Encoding \"gzip,deflate\" env=HAVE_Accept-Encoding\n        \u003c/IfModule\u003e\n    \u003c/IfModule\u003e\n\n    # Compress all output labeled with one of the following MIME-types\n    # (for Apache versions below 2.3.7, you don't need to enable `mod_filter`\n    #  and can remove the `\u003cIfModule mod_filter.c\u003e` and `\u003c/IfModule\u003e` lines\n    #  as `AddOutputFilterByType` is still in the core directives).\n    \u003cIfModule mod_filter.c\u003e\n        AddOutputFilterByType DEFLATE application/atom+xml \\\n                                      application/javascript \\\n                                      application/json \\\n                                      application/rss+xml \\\n                                      application/vnd.ms-fontobject \\\n                                      application/x-font-ttf \\\n                                      application/x-web-app-manifest+json \\\n                                      application/xhtml+xml \\\n                                      application/xml \\\n                                      font/opentype \\\n                                      image/svg+xml \\\n                                      image/x-icon \\\n                                      text/css \\\n                                      text/html \\\n                                      text/plain \\\n                                      text/x-component \\\n                                      text/xml\n    \u003c/IfModule\u003e\n\n\u003c/IfModule\u003e\n```\n[Source](https://github.com/h5bp/server-configs-apache)\n\n\n### Set Expires Headers\n_Expires headers_ tell the browser whether they should request a specific file from the server or just grab it from the cache. It is advisable to set static content's expires headers to something far in the future.\n\nIf you don’t control versioning with filename-based cache busting, consider lowering the cache time for resources like CSS and JS to something like 1 week. [Source](https://github.com/h5bp/server-configs-apache)\n``` apacheconf\n\u003cIfModule mod_expires.c\u003e\n    ExpiresActive on\n    ExpiresDefault                                      \"access plus 1 month\"\n\n  # CSS\n    ExpiresByType text/css                              \"access plus 1 year\"\n\n  # Data interchange\n    ExpiresByType application/json                      \"access plus 0 seconds\"\n    ExpiresByType application/xml                       \"access plus 0 seconds\"\n    ExpiresByType text/xml                              \"access plus 0 seconds\"\n\n  # Favicon (cannot be renamed!)\n    ExpiresByType image/x-icon                          \"access plus 1 week\"\n\n  # HTML components (HTCs)\n    ExpiresByType text/x-component                      \"access plus 1 month\"\n\n  # HTML\n    ExpiresByType text/html                             \"access plus 0 seconds\"\n\n  # JavaScript\n    ExpiresByType application/javascript                \"access plus 1 year\"\n\n  # Manifest files\n    ExpiresByType application/x-web-app-manifest+json   \"access plus 0 seconds\"\n    ExpiresByType text/cache-manifest                   \"access plus 0 seconds\"\n\n  # Media\n    ExpiresByType audio/ogg                             \"access plus 1 month\"\n    ExpiresByType image/gif                             \"access plus 1 month\"\n    ExpiresByType image/jpeg                            \"access plus 1 month\"\n    ExpiresByType image/png                             \"access plus 1 month\"\n    ExpiresByType video/mp4                             \"access plus 1 month\"\n    ExpiresByType video/ogg                             \"access plus 1 month\"\n    ExpiresByType video/webm                            \"access plus 1 month\"\n\n  # Web feeds\n    ExpiresByType application/atom+xml                  \"access plus 1 hour\"\n    ExpiresByType application/rss+xml                   \"access plus 1 hour\"\n\n  # Web fonts\n    ExpiresByType application/font-woff2                \"access plus 1 month\"\n    ExpiresByType application/font-woff                 \"access plus 1 month\"\n    ExpiresByType application/vnd.ms-fontobject         \"access plus 1 month\"\n    ExpiresByType application/x-font-ttf                \"access plus 1 month\"\n    ExpiresByType font/opentype                         \"access plus 1 month\"\n    ExpiresByType image/svg+xml                         \"access plus 1 month\"\n\u003c/IfModule\u003e\n```\n\n### Turn eTags Off\nBy removing the `ETag` header, you disable caches and browsers from being able to validate files, so they are forced to rely on your `Cache-Control` and `Expires` header. [Source](http://www.askapache.com/htaccess/apache-speed-etags.html)\n``` apacheconf\n\u003cIfModule mod_headers.c\u003e\n    Header unset ETag\n\u003c/IfModule\u003e\nFileETag None\n```\n\n## Miscellaneous\n\n### Set PHP Variables\n``` apacheconf\nphp_value \u003ckey\u003e \u003cval\u003e\n\n# For example:\nphp_value upload_max_filesize 50M\nphp_value max_execution_time 240\n```\n\n### Custom Error Pages\n``` apacheconf\nErrorDocument 500 \"Houston, we have a problem.\"\nErrorDocument 401 http://error.example.com/mordor.html\nErrorDocument 404 /errors/halflife3.html\n```\n\n### Force Downloading\nSometimes you want to force the browser to download some content instead of displaying it.\n``` apacheconf\n\u003cFiles *.md\u003e\n    ForceType application/octet-stream\n    Header set Content-Disposition attachment\n\u003c/Files\u003e\n```\n\nNow there is a yang to this yin:\n\n### Prevent Downloading\nSometimes you want to force the browser to display some content instead of downloading it.\n``` apacheconf\n\u003cFilesMatch \"\\.(tex|log|aux)$\"\u003e\n    Header set Content-Type text/plain\n\u003c/FilesMatch\u003e\n```\n\n### Allow Cross-Domain Fonts\nCDN-served webfonts might not work in Firefox or IE due to [CORS](https://en.wikipedia.org/wiki/Cross-origin_resource_sharing). This snippet solves the problem.\n``` apacheconf\n\u003cIfModule mod_headers.c\u003e\n    \u003cFilesMatch \"\\.(eot|otf|ttc|ttf|woff|woff2)$\"\u003e\n        Header set Access-Control-Allow-Origin \"*\"\n    \u003c/FilesMatch\u003e\n\u003c/IfModule\u003e\n```\n[Source](https://github.com/h5bp/server-configs-apache/issues/32)\n\n### Auto UTF-8 Encode\nYour text content should always be UTF-8 encoded, no?\n``` apacheconf\n# Use UTF-8 encoding for anything served text/plain or text/html\nAddDefaultCharset utf-8\n\n# Force UTF-8 for a number of file formats\nAddCharset utf-8 .atom .css .js .json .rss .vtt .xml\n```\n[Source](https://github.com/h5bp/server-configs-apache)\n\n### Switch to Another PHP Version\nIf you’re on a shared host, chances are there are more than one version of PHP installed, and sometimes you want a specific version for your website. The following snippet should switch the PHP version for you.\n\n``` apacheconf\nAddHandler application/x-httpd-php56 .php\n\n# Alternatively, you can use AddType\nAddType application/x-httpd-php56 .php\n```\n\n### Disable Internet Explorer Compatibility View\nCompatibility View in IE may affect how some websites are displayed. The following snippet should force IE to use the Edge Rendering Engine and disable the Compatibility View.\n\n``` apacheconf\n\u003cIfModule mod_headers.c\u003e\n    BrowserMatch MSIE is-msie\n    Header set X-UA-Compatible IE=edge env=is-msie\n\u003c/IfModule\u003e\n```\n\n### Serve WebP Images\nIf [WebP images](https://developers.google.com/speed/webp/?csw=1) are supported and an image with a .webp extension and the same name is found at the same place as the jpg/png image that is going to be served, then the WebP image is served instead.\n\n``` apacheconf\nRewriteEngine On\nRewriteCond %{HTTP_ACCEPT} image/webp\nRewriteCond %{DOCUMENT_ROOT}/$1.webp -f\nRewriteRule (.+)\\.(jpe?g|png)$ $1.webp [T=image/webp,E=accept:1]\n```\n[Source](https://github.com/vincentorback/WebP-images-with-htaccess)\n","funding_links":["https://github.com/sponsors/phanan","https://opencollective.com/koel"],"categories":["Back-End Development","Technical","Uncategorized","Apache","Others","Server Configuration","System administration","Back-end Development","HarmonyOS","miscellaneous","Podcast","Live Site:   [searchAwesome](https://search-awesome.vercel.app/)","网络服务","awesome","后端开发","awesome-list","Themed Directories"],"sub_categories":["Uncategorized","Chess :chess_pawn:","ramanihiteshc@gmail.com","Windows Manager","网络服务_其他","Misc"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphanan%2Fhtaccess","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphanan%2Fhtaccess","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphanan%2Fhtaccess/lists"}