How to use htaccess in WordPress.

Source: Internet
Author: User

Some users may not yet know what the. htaccess file is, so let's start by explaining the role of htaccess. In the root directory of WordPress, you will see a file named. htaccess, which can be created by the system or edited by the user itself. It is a configuration file in the Apache server, it is responsible for the configuration of the Web page in the related directory, it is very important to implement permanent link (permalink) in WordPress. At the same time, we can also implement: page 301 redirect, custom 404 error page, change file extension, allow/block specific user or directory access, prohibit directory list, configure default document and other functions. Let's take a look at how to prepare htaccess.

The following is the default htaccess content in WordPress. We hit look at the role of each line.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L] </IfModule>

The first line is used to determine if the rewrite module is installed in Apache. If the rewrite module is present, "rewriteengine on" is used to open the rewrite module.

"Rewritebase/" is used to denote the part of Apache that the URL overrides. For example, the site is http://domain.com/. When Rewritebase is followed by "/", it means that all sub-paths below the primary domain name are overridden, and if "/blog/" is only the part after http://domain.com/blog/is overridden.

Rewritecond represents some of the conditions that the rewrite URL should satisfy, and the first parameter after Rewriterule represents the rule (regular expression) to be overridden by the URL that satisfies, and the second parameter represents the rewritten URL. The last one [L] indicates that the current rule is the last rule, and the override of the rule is stopped after parsing. (There are many other parameters that can be supported in the box, as described below)

So "Rewriterule ^index\.php$–[l]" indicates that if the URL of the current request is exactly index.php, do not make any subsequent judgments.

"Rewritecond%{request_filename}!-f" and "Rewritecond%{request_filename}!-d" indicate that the current URL is not a file (-f) or a directory (-D). When both conditions are met, the last article "Rewriterule." /index.php [L] "will rewrite all requests to index.php.

As we can see from here, WordPress defaults to all the URLs that do not refer to static files or directories are all rewritten to index.php, and then the request is processed uniformly by index.php.

Just now we mentioned the function of [L], in fact Rewriterule also supports many other parameters, including:

    • R forces external redirection, which can be followed by a 301 or 302 jump.
    • F disables the URL and returns the 403HTTP status code.
    • G force URL is gone, return 410HTTP status code.
    • P enforces the use of proxy forwarding.
    • L indicates that the current rule is the last rule, stopping the rewrite of the rule after parsing.
    • N re-run the rewrite process starting with the first rule.
    • C is associated with the next rule.
    • The T=mime-type (Force MIME type) enforces the MIME type.
    • NS is used only for internal sub-requests.
    • NC is case insensitive.
    • QSA the append request string.
    • NE does not escape special characters in the output.
Some tips for using htaccess in WordPress

It says a little bit about WordPress. The basic use of httaccess, in fact, the use of htaccess files can also help us do a lot of other work. Let me introduce some of the commonly used features, and more readers can try them on their own.

Domain Jump

In some cases, we would like to jump the domain.com domain name to www.domain.com. Htaccess can come in handy at this time. Add code similar to the following in the. htaccess file: (The following code is also used when the site changes the domain name)
RewriteCond %{HTTP_HOST} ^domain\.com [NC] RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
Similarly, you can also use htacess to change the directory structure of the URL, which is especially useful when the category name is replaced:
RewriteRule ^/?old_directory/([a-z/.]+)$ new_directory/$1 [R=301,L]
Note: The 301 after R indicates that the jump is converted to a permanent jump, or it can be a temporary jump (302) According to the actual situation.

Anti-theft chain

Picture of the hotlinking may be a lot of webmaster the most headache things, their pictures were taken without saying, will free up their own site resources, the use of htaccess files can also be used to achieve the function of anti-theft chain. The code is as follows:
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://domain.com/.*$ [NC] RewriteCond %{HTTP_REFERER} !^http://domain.com$ [NC] RewriteCond %{HTTP_REFERER} !^http://www.domain.com/.*$ [NC] RewriteCond %{HTTP_REFERER} !^http://www.domain.com$ [NC] RewriteRule .*\.(gif|jpg|jpeg|png|bmp|swf|mp3|wav|zip|rar)$ http://www.domain.com/404.html [R,NC]

This code will determine the referer of the request, if the link is not from the site hit, the default will jump to a 404 page. The file type for the anti-theft chain is specified in Rewriterule.

Prevent spam Reviews

Examples of htaccess can also be used to prevent some unidentified spam comments:
RewriteEngine On
RewriteCond %{REQUEST_METHOD} POST
RewriteCond %{REQUEST_URI} .wp-comments-post\.php*
RewriteCond %{HTTP_REFERER} !.*domain.com.* [OR] RewriteCond %{HTTP_USER_AGENT} ^$
RewriteRule (.*) ^http://domain.com/$ [R=301,L]

This code, determine whether a POST request is issued by the browser (to determine whether http_user_agent is empty), and refer whether the site, as long as any one of the conditions are not satisfied, will request to automatically jump to the home page.

Note that this code only applies to WordPress, in fact, the framework can be based on its specific circumstances to modify the wp-comments-post to the corresponding URL.

Only allow yourself to access WordPress backstage

If you have a fixed IP, then you can set up in htaccess only you can access WordPress backstage. create a new. htaccess file in the Wp-admin directory with the following code:
AuthUserFile /dev/null
AuthGroupFile /dev/null
AuthType Basic
<LIMIT GET>
order deny,allow
deny from all

# xx.xx.xx.xx为您的IP,您也以设定多个IP。
allow from xx.xx.xx.xx
allow from xx.xx.xx.xxx
</LIMIT>

Note: For this code, must be in the Wp-admin folder to establish a new htaccess, a lot of pit Dad's article did not tell you this, directly add to the site root directory under the htacess words ... If you want your entire website to be accessible only on your own, try it.

Block certain IPs from accessing their sites

If some of the spam sites robot always crawl your site's data, you can block these IPs in htaccess.
<Limit GET POST>
order allow,deny
deny from xx.xx.xx.xx
allow from all
</Limit>

Similarly, changing deny and allow allows only certain IP sites to be accessed, if you really want to.

Judging the browser

Browser compatibility can be said to be the most heartache of front-end engineers. Instead of writing a variety of hack in CSS, it's better to judge the browser in htacess and point directly to a different CSS file.
RewriteCond %{REQUEST_URI} index\.css*
RewriteCond %{HTTP_USER_AGENT} "^Mozilla/4.0$"
RewriteRule ^(.*)$ http://domain.com/firefox.css

Similarly, it is easy to use htacess to determine whether a visitor is holding a mobile device.
Rewriteengine on
Rewritecond%{http_user_agent} "Windows CE" [Nc,or] Rewritecond%{http_user_agent} "NetFront" [Nc,or] Rewritecond%{HTTP _user_agent} "Palm OS" [Nc,or] Rewritecond%{http_user_agent} "Blazer" [Nc,or] Rewritecond%{http_user_agent} "Elaine" [N C,or] Rewritecond%{http_user_agent} "^wap.*$" [Nc,or] Rewritecond%{http_user_agent} "Plucker" [NC,OR] RewriteCond%{ Http_user_agent} "Vodafone" [Nc,or] Rewritecond%{http_user_agent} "IPhone" [Nc,or] Rewritecond%{http_user_agent} " Nokia "[Nc,or] Rewritecond%{http_user_agent}" Symbian "[Nc,or] Rewritecond%{http_user_agent}" Opera Mini "[Nc,or] Rewri Tecond%{http_user_agent} "BlackBerry" [Nc,or] Rewritecond%{http_user_agent} "J2ME" [Nc,or] Rewritecond%{HTTP_USER_ AGENT} "MIDP" [Nc,or] Rewritecond%{http_user_agent} "HTC" [Nc,or] Rewritecond%{http_user_agent} "Java" [Nc,or] Rewritec Ond%{http_user_agent} "Sony" [Nc,or] Rewritecond%{http_user_agent} "Android" [Nc,or] Rewritecond%{http_user_agent} " AvantGo "[NC] Rewriterule (. *) Http://mobi.domain.com/[R,l]

Because a bit more, for the convenience of reading, there is a general browser user_agent at the end of the article. Interested readers can go and have a look.

Prohibit access to specific file types or files

Some specific files, or some files, we may not want to let users access to, you can use the following code:
<Files secretfile.jpg>
order allow,deny
deny from all
</Files>

<FilesMatch ".(htaccess|htpasswd|ini|phps|fla|psd|log|sh)$">
Order Allow,Deny
Deny from all
</FilesMatch>

Determine the language of the browser

If you want to jump to a different page depending on your browser's language settings, you can use the following code:
RewriteCond %{HTTP:Accept-Language} ^zh-cn.*$ [NC] RewriteRule ^/?$ index_cn.htm [R=301,L]

Summarize

Here, in fact, there are many uses of htaccess, readers can be based on the above example of their own to explore the new use of htaccess. Note, however, that you must be careful when modifying htaccess, because one but wrong writing may cause the entire site to be inaccessible.

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.