18 useful. htaccess file usage tips,

Source: Internet
Author: User
Tags php error wordpress blog

18 useful. htaccess file usage tips,

. Htaccess is a special configuration file in Apache on the Web server. It controls many behaviors on the server. We can use it to do many things, such as setting access permissions and URL redirection. This article shows you 18 tips for using. htaccess files.

Tip: When editing a. htaccess file, make sure that you back up the file first. This file is very important, and an error may cause unimaginable consequences!

1. Remove WWW from the blog website.

It is said that this is conducive to SEO. Add the following code to the. htaccess file, and all URLs with www. will be redirected to those without www. You need to change ijinfa.cn to your domain name!

?
123 RewriteEngine OnRewriteCond %{HTTP_HOST} !^ijinfa.cn$ [NC]RewriteRule ^(.*)$ http://ijinfa.cn/$1 [L,R=301]

Source: css-tricks

2. Forcibly use the URL in WWW. Format

Change www.ijinfa.cn to your domain name!

?
123 RewriteEngine OnRewriteCond %{HTTP_HOST} ^ijinfa.cn [NC]RewriteRule ^(.*)$ http://www.ijinfa.cn/$1 [L,R=301]

Source: css-tricks

3. Set image anti-leech?
123456 RewriteEngine On#Replace ?mysite\.com/ with your blog urlRewriteCond %{HTTP_REFERER} !^http://(.+\.)?mysite\.com/ [NC]RewriteCond %{HTTP_REFERER} !^$#Replace /images/nohotlink.jpg with your "don't hotlink" image urlRewriteRule .*\.(jpe?g|gif|bmp|png)$ /images/nohotlink.jpg [L]
4. Redirect all feeds in the WordPress blog to feedburner

Replace the http://feedburner.com/yourfeed/ with your feedburner ID.

?
1234 <IfModule mod_alias.c> RedirectMatch 301 /feed/(atom|rdf|rss|rss2)/?$ http://feedburner.com/yourfeed/ RedirectMatch 301 /comments/feed/(atom|rdf|rss|rss2)/?$ http://feedburner.com/yourfeed/</IfModule>

Source: wprecipes

5. custom error page

You need to create some html files by yourself. When visitors access non-existent pages, these html pages will be displayed.

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

Source: css-tricks

6. Force download of a specific object

If your website provides the download of mp3s, eps, or xls files, you can use the following code to forcibly download them if you do not want the browser to decide whether to download them.

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

Source: givegoodweb

7. PHP error records

Create a php_error.log file on your server, and put the following code into the. htaccess file. Remember to modify the file path in Row 7. This is a very useful function.

?
1234567 # display no errs to userphp_flag display_startup_errors offphp_flag display_errors offphp_flag html_errors off# log to filephp_flag log_errors onphp_value error_log /location/to/php_error.log

Source: css-tricks

8. Remove the file extension from the URL

File extensions are useful to Web developers, but they are useless to visitors and you do not want them to see them. Use the following method to remove them. The following method removes the html extension. You can change it to php, htm, asp, and so on.

?
12345 RewriteEngine onRewriteCond %{REQUEST_FILENAME} !-dRewriteCond %{REQUEST_FILENAME}\.<span style="color: #ff0000;">html</span> -fRewriteRule ^(.*)$ $1.<span style="color: #ff0000;">html</span># Replace html with your file extension, eg: php, htm, asp

Source: eisabainyo

9. Disable Automatic listing of system file directories

If an index file does not exist in a file directory on your website, the server will automatically list all files in the current directory. Generally, you do not want others to see it. Use the following code to disable the directory from being listed.

?
1 Options -Indexes
10. Compress static files and accelerate page downloads

This will reduce server bandwidth consumption to a certain extent and speed up reader access.

?
1234 AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml text/javascript text/css application/x-javascriptBrowserMatch ^Mozilla/4 gzip-only-text/htmlBrowserMatch ^Mozilla/4.0[678] no-gzipBrowserMatch bMSIE !no-gzip !gzip-only-text/html
11. automatically add UTF-8 encoding to the file

Some specific files, such as html, htm, css, or js, may sometimes forget to add the <meta http-equiv = "Content-Type"> encoding tag on the page, to prevent code problems, you can enable the page to automatically generate such tags.

?
123 <FilesMatch "\.(htm|html|css|js)$">ForceType 'text/html; charset=UTF-8'</FilesMatch>

Source: askapache

12. Remove the/category/directory name from the WordPress website.

By default, the permanent link of the WordPress category directory is displayed as follows:

?
1 http://www.catswhocode.com/blog/category/wordpress

The directory name/category/is useless and can be removed, just add the line up and down in the. htaccess file (where http://www.yourblog.com/is your domain name ):

?
1 RewriteRule ^category/(.+)$ http://www.yourblog.com/$1 [R=301,L]

Save the modified file, and the link form of the category directory will become as follows:

?
1 http://www.yourblog.com/blog/wordpress

Source: wprecipes

13. Use browser cache

One effective way to speed up blog is to force browser cache. Add the following statement to the. htaccess file:

?
1234567 FileETag MTime Size<ifmodule mod_expires.c>  <filesmatch "\.(jpg|gif|png|css|js)$">       ExpiresActive on       ExpiresDefault "access plus 1 year"   </filesmatch></ifmodule>

Source: wordpress-tutoriel

14. Redirect the permanent link with the published date and log name to the/% postname %/format

This/% postname %/is the log name, that is, the log name is used as a permanent link. First, log on to the WordPress background and go to Settings> permanent link. In the custom permanent link field, enter
/% Postname %/, your permanent blog link will become as follows:

?
1 http://www.yourblog.com/name-of-the-post

Next, we will use 301 redirection to direct all old links to the new link above. Open the. htaccess file under the WordPress root directory and add the following statement:

?
1 RedirectMatch 301 /([0-9]+)/([0-9]+)/([0-9]+)/(.*)$ http://www.domain.com/$4

Done!

Source: wprecipes

15. Reject comments from others

The principle is to prohibit (robot) non-human access to wp-comments-post.php files, which can effectively prevent a large number of spam comments. Change yourblog.com on the fourth line to Your URL.

?
123456 RewriteEngine OnRewriteCond %{REQUEST_METHOD} POSTRewriteCond %{REQUEST_URI} .wp-comments-post\.php*RewriteCond %{HTTP_REFERER} !.*yourblog.com.* [OR]RewriteCond %{HTTP_USER_AGENT} ^$RewriteRule (.*) ^http://%{REMOTE_ADDR}/$ [R=301,L]

Source: wprecipes

16. Redirect visitors to a maintenance mode page

When upgrading a blog or modifying a topic, it is not a good thing to let visitors see these changes (especially code errors), or even cause security problems, it is necessary to direct all visitors to a specified maintenance mode page. The following method uses redirection to direct the reader to a page called “maintenance.html. The IP address of your blog is in the third line.

?
1234 RewriteEngine onRewriteCond %{REQUEST_URI} !/maintenance.html$RewriteCond %{REMOTE_ADDR} !^123\.123\.123\.123RewriteRule $ /maintenance.html [R=302,L]

Source: woueb

17. Only allow your own IP address to access the wp-admin directory

Unless it is a multi-author blog, if you are using a static IP address, you can set that only you can access the wp-admin directory, use"Allow from xx. xx. xxx. xx"In this format, multiple static IP addresses each occupy one row.

?
123456789 AuthUserFile /dev/nullAuthGroupFile /dev/nullAuthName "Example Access Control"AuthType Basic<LIMIT GET>order allow, denydeny from allallow from xx.xx.xx.xx</LIMIT>

Source: reubenyau

18. Blacklist specific IP addresses

In the format of "deny from xxx. xx. xxx. xxx", different IP addresses take up five lines. This method can be used to prevent spam comments or visitors you want to prohibit access.

?
12345 <Limit GET POST>order allow,denydeny from 222.69.116.166allow from all</Limit>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.