Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the 10 practical .htaccess code snippets?

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail what the 10 practical.htaccess code snippets are. The content of the article is of high quality. Therefore, Xiaobian shares it with you as a reference. I hope that after reading this article, you will have a certain understanding of relevant knowledge.

Here are 10 useful.htaccess code snippets.

1. Except www in URL

For SEO reasons, you might want to remove the www prefix from URLs. The following code implements this functionality and redirects all addresses with www to non-www first-level domains.

RewriteEngine On RewriteCond %{HTTP_HOST} !^ mangguo.org$ [NC] RewriteRule ^(.*)$ http://mangguo.org/$1 [L,R=301]

Source: css-tricks.com/snippets/htaccess/www-no-www/

2. Prevent chain theft

Chain theft is usually considered a shameful act. When you are stolen by others, others will use your expensive bandwidth for free, not stingy, but bandwidth costs can not afford to hurt ah can not afford to hurt. To prevent chain theft, simply add the following code:

RewriteEngine On #Will it? mangguo\.org/Replace with your blog address RewriteCond %{HTTP_REFER} !^ http://(.+\.)? mangguo\.org/ [NC] RewriteCond %{HTTP_REFERER} !^$ #Replace/images/nohotlink.jpg with RewriteRule .*\. (jpe? g|gif|bmp|png)$ /images/nohotlink.jpg [L]

Redirect WordPress RSS feeds to Feedburner

Most bloggers use Feedburner to host RSS feeds for statistical analysis of blog readings. If you use WordPress, you should redirect all RSS feeds to Feedburner feeds. Modify the second and third lines of code and copy the code into.htaccess.

RedirectMatch 301 /feed/(atom|rdf|rss|rss2)/?$ http://feeds.feedburner.com/mangguo/ RedirectMatch 301 /comments/feed/(atom|rdf|rss|rss2)/?$ http://feeds.feedburner.com/mangguo/

Source: www.wprecipes.com/how-to-redirect-wordpress-rss-feeds-to-feedburner-with-htaccess

4. Create custom error pages

Tired of old error pages? Then practice making custom error pages yourself. Upload these personalized error pages to the host and add the following code:

ErrorDocument 400 /errors/badrequest.html ErrorDocument 401 /errors/authreqd.html ErrorDocument 403 /errors/forbid.html ErrorDocument 404 /errors/notfound.html ErrorDocument 500 /errors/serverr.html

Source: css-tricks.com/snippets/htaccess/custom-error-pages/

5. Forced download of specified files

When offering downloads like MP3s, eps or xls, you may want to force the client to download rather than letting the browser decide whether to download.

ForceType application/octet-stream Header set Content-Disposition attachment ForceType application/octet-stream Header set Content-Disposition attachment

Source: www.givegoodweb.com/post/30/forcing-a-download-with-apache-and-htaccess

6. Log PHP errors

This code creates a php_error.log file on the server and writes the error log for the PHP file to that log file.

# display no errs to user php_flag display_startup_errors off php_flag display_errors off php_flag html_errors off # log to file php_flag log_errors on php_value error_log /location/to/php_error.log

Source: css-tricks.com/snippets/htaccess/php-error-logging/

7. Remove file extensions from URLs

File extensions may be useful to developers, but to visitors, they are useless. This code removes the.html suffix from html files. Of course you can also use it to remove other types of files, such as php.

RewriteEngine on RewriteCond %{REQUEST_FILENAME} !- d RewriteCond %{REQUEST_FILENAME}\.html -f RewriteRule ^(.*)$ $1.html # Replace html with your file extension, eg: php, htm, asp

Source: eisabainyo.net/weblog/2007/08/19/removing-file-extension-via-htaccess

8. Prevent list of directories

On your web server, when a directory has no index files, apache automatically creates an index list for all files in the current directory. If you don't want others to see these files, you can add the following code to block automatic directory listings.

Options -Indexes Reduce page size by compressing static resources

Data transmission in the browser can be compressed, and the client can decompress compressed data sent by the server. This code will amicably reduce your page size and save bandwidth expenses.

AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml text/javascript text/css application/x-javascript BrowserMatch ^Mozilla/4 gzip-only-text/html BrowserMatch ^Mozilla/4.0[678] no-gzip BrowserMatch bMSIE ! no-gzip ! gzip-only-text/html

10. Automatically add utf-8 encoding to files

To avoid encoding problems, you can force encoding through.htaccess files. This ensures that HTML documents are always rendered correctly, even if you forget to add statements.

AddDefaultCharset UTF-8 About 10 practical.htaccess code snippets are what to share here, I hope the above content can be of some help to everyone, you can learn more knowledge. If you think the article is good, you can share it so that more people can see it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report