A collection of practical configuration examples for the.htaccess file on the Apache server

  • 2020-05-15 02:44:02
  • OfStack

What is htaccess?

htaccess (hypertext access, hypertext access) is a file that provides site owners with options to control server environment variables and other parameters to enhance the functionality of their site. These files can be in any one of the directories in the site's directory tree and provide functionality to that directory as well as the files and subdirectories in that directory.

What are these functions? These are actually instructions from the server, such as lines that command the server to perform a specific task. These commands are only valid for files and subdirectories in the directory where the file resides. These files are hidden by default because all operating systems and web servers are configured to ignore them by default, but if you look at the hidden files, you can see these special files. Subsequent sections will discuss what types of parameters can be controlled.

Note: if the htaccess file stored in the/apache/home/www Gunjit/directory, then it will be provided to all files and subdirectories in this directory command, but if the directory contains a called/Gunjit images/subdirectory, and the subdirectory also has 1. htaccess file, the command in the this subdirectory will cover in the parent directory htaccess file (or the upper more files in the directory hierarchy) provided by the command.

Apache Server and.htaccess files

Apache HTTP Server, commonly known as Apache, is named in honor of a native American tribe with superior war strategy skills. It is based on NCSA HTTPd server, which is a cross-platform Web server built with C/C++ and XML. It has played a key role in the growth and development of the world wide web.

It is most commonly used for UNIX, but Apache can also be used on a variety of platforms, including FreeBSD, Linux, Windows, Mac OS, Novel Netware, and so on. In 2009, Apache became the first server to serve more than 100 million sites.

The Apache server allows each user in the www/ directory to have a separate.htaccess file. Although these files are hidden, they can be made visible if needed. There can be many subdirectories in the www/ directory, each named by user name or owner name, containing one site. In addition, you can have one.htaccess file per subdirectory, as described earlier for configuring files in subdirectories.

Here's how to configure the htaccess file on the Apache server.

Configuration on the Apache server

Here are two cases:

Host the site on your own server

In this case, if the.htaccess file is not enabled, you can find part of it in http.conf (the default configuration file for the Apache HTTP daemon).


<Directory"/var/www/htdocs">

Position the following line


AllowOverrideNone

Change to


AllowOverrideAll

Now, restart Apache and you have.htaccess enabled.

11 practical Apache.htaccess configurations
1. Force the suffix backslash
Putting a backslash at the end of URL seems to favor SEO :)


<IfModule mod_rewrite.c> 
 RewriteCond %{REQUEST_URI} /+[^\.]+$ 
 RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L] 
</IfModule> 

2. Preventing hotlinking
Save your precious bandwidth!


RewriteEngine On 
#Replace ?mysite\.com/ with your blog url 
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?mysite\.com/ [NC] 
RewriteCond %{HTTP_REFERER} !^$ 
#Replace /images/nohotlink.jpg with your "don't hotlink" image url 
RewriteRule .*\.(jpe?g|gif|bmp|png)$ /images/nohotlink.jpg [L] 

3. Redirect mobile devices
When adding mobile device support to your site, it's best to redirect mobile device access to a customized page


RewriteEngine On 
RewriteCond %{REQUEST_URI} !^/m/.*$ 
RewriteCond %{HTTP_ACCEPT} "text/vnd.wap.wml|application/vnd.wap.xhtml+xml" [NC,OR] 
RewriteCond %{HTTP_USER_AGENT} "acs|alav|alca|amoi|audi|aste|avan|benq|bird|blac|blaz|brew|cell|cldc|cmd-" [NC,OR] 
RewriteCond %{HTTP_USER_AGENT} "dang|doco|eric|hipt|inno|ipaq|java|jigs|kddi|keji|leno|lg-c|lg-d|lg-g|lge-" [NC,OR] 
RewriteCond %{HTTP_USER_AGENT} "maui|maxo|midp|mits|mmef|mobi|mot-|moto|mwbp|nec-|newt|noki|opwv" [NC,OR] 
RewriteCond %{HTTP_USER_AGENT} "palm|pana|pant|pdxg|phil|play|pluc|port|prox|qtek|qwap|sage|sams|sany" [NC,OR] 
RewriteCond %{HTTP_USER_AGENT} "sch-|sec-|send|seri|sgh-|shar|sie-|siem|smal|smar|sony|sph-|symb|t-mo" [NC,OR] 
RewriteCond %{HTTP_USER_AGENT} "teli|tim-|tosh|tsm-|upg1|upsi|vk-v|voda|w3cs|wap-|wapa|wapi" [NC,OR] 
RewriteCond %{HTTP_USER_AGENT} "wapp|wapr|webc|winw|winw|xda|xda-" [NC,OR] 
RewriteCond %{HTTP_USER_AGENT} "up.browser|up.link|windowssce|iemobile|mini|mmp" [NC,OR] 
RewriteCond %{HTTP_USER_AGENT} "symbian|midp|wap|phone|pocket|mobile|pda|psp" [NC] 
#------------- The line below excludes the iPad 
RewriteCond %{HTTP_USER_AGENT} !^.*iPad.*$ 
#------------- 
RewriteCond %{HTTP_USER_AGENT} !macintosh [NC] #*SEE NOTE BELOW 
RewriteRule ^(.*)$ /m/ [L,R=302] 

4. Force the browser to download the specified file type
You can force the browser to download certain types of files instead of reading and opening them, such as MP3, XLS.


<Files *.xls> 
 ForceType application/octet-stream 
 Header set Content-Disposition attachment 
</Files> 
<Files *.eps> 
 ForceType application/octet-stream 
 Header set Content-Disposition attachment 
</Files> 

5. Cross-domain font embedding for firefox
Firefox does not allow you to embed an external font, so the. htaccess snippet below can get around this restriction


<FilesMatch "\.(ttf|otf|eot|woff)$"> 
<IfModule mod_headers.c> 
  Header set Access-Control-Allow-Origin "http://yourdomain.com" 
</IfModule> 
</FilesMatch> 

6. Use the.htaccess cache to speed up your site
This is probably the most useful piece of code. This code can help you greatly improve the speed of your website!


# 1 YEAR 
<FilesMatch "\.(ico|pdf|flv)$"> 
Header set Cache-Control "max-age=29030400, public" 
</FilesMatch> 
# 1 WEEK 
<FilesMatch "\.(jpg|jpeg|png|gif|swf)$"> 
Header set Cache-Control "max-age=604800, public" 
</FilesMatch> 
# 2 DAYS 
<FilesMatch "\.(xml|txt|css|js)$"> 
Header set Cache-Control "max-age=172800, proxy-revalidate" 
</FilesMatch> 
# 1 MIN 
<FilesMatch "\.(html|htm|php)$"> 
Header set Cache-Control "max-age=60, private, proxy-revalidate" 
</FilesMatch> 

7. Stop spam from the WordPress blog
Still struggling with spam? You can solve this problem with the Akismet plugin, but the.htaccess file is more straightforward: it prevents spam bots from accessing the wp-comments-post.php file


<IfModule mod_rewrite.c> 
RewriteEngine On 
RewriteCond %{REQUEST_METHOD} POST 
RewriteCond %{REQUEST_URI} .wp-comments-post\.php* 
RewriteCond %{HTTP_REFERER} !.*yourdomainname.* [OR] 
RewriteCond %{HTTP_USER_AGENT} ^$ 
RewriteRule (.*) ^http://%{REMOTE_ADDR}/$ [R=301,L] 
</IfModule> 

8. Redirect different feed formats to a unified 1 format
Many years ago, there were many different feed formats, such as RSS, Atom, RDF, and so on. But now the RSS is absolutely dominant. The following code allows you to redirect different feed formats to the same feed. This code can be used directly on the WordPress blog.


AllowOverrideNone
0

9. Configure HTML5 videos for the website
HTML5 gives us video playback without Flash, but you must configure your server to provide the latest HTML5 video playback.


AllowOverrideNone
1

10. Record PHP error
Displaying the PHP error on the page is embarrassing and not safe, but the following code will record the PHP error in the.log file instead of on the page.


AllowOverrideNone
2

11. Run PHP in the JavaScript code
It is sometimes useful to insert PHP code in JS, such as reading a database. The following code allows you to run PHP in JS.


AddType application/x-httpd-php .js 
AddHandler x-httpd-php5 .js 
 
<FilesMatch "\.(js|php)$"> 
SetHandler application/x-httpd-php 
</FilesMatch> 

12. 404 page jump, jump to 404.php, according to the url record there is no page path


AllowOverrideNone
4


Related articles: