10 great tricks with .htaccess for WordPress

    Attention!
    Before modifying the .htaccess file, be sure to backup it.


    1 - Redirect WordPress RSS feed to feedburner using .htaccess
    Why some webmasters do not use feedburner? After all, this is such a wonderful tool for monitoring RSS subscriptions. The problem is that you have to fix template files with your hands. This technique will help save your time.
    And don't forget to fix line 6 on your code

    
     Rewriteengine on
     RewriteCond% {HTTP_USER_AGENT}! FeedBurner [NC]
     RewriteCond% {HTTP_USER_AGENT}! FeedValidator [NC]
     RewriteRule ^ feed /? ([_ 0-9a-z -] +)? /? $ Http: //feeds2.feedburner.com/wordpress [R = 302, NC, L]
    




    2 - Delete / category / from the path in the WordPress address
    By default, categories in WordPress are displayed like this: https: //www.wordpress.com/blog/category/wordpress
    This is not very beautiful, and the address looks a bit long.
    Now you will learn how to fix it with .htaccess.

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

    Now the categories will look like
    this: https://www.wordpress.com/blog/wordpress

    3 - Using the browser cache
    A very good way to optimize your blog is to use the browser cache. This code improves browser caching of static files.
    Upon repeated request to a file that has not changed, the client will receive a response 304, not the contents of the file.

    FileETag MTime Size
    
           ExpiresActive on
           ExpiresDefault "access plus 1 year"
       


    4 - Compression of static data
    This code will reduce the amount of data transmitted between the server and the user by compressing it.

    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
    


    5 - Redirecting permalinks based on Day and name to /% postname% /
    First go to the WordPress admin area, go to Settings → Permalinks and select custom.
    Fill in the field with /% postname% /.
    Now the permalinks will look like this: Http: //www.yourblog.com/name-of-the-post

    Now we need to redirect all the old links to new permalinks.
    Add the following lines to .htaccess:

    RedirectMatch 301 /([0-9.06.2012+)/([0-9.06.2012+)/([0-9.BIZ+)/(.*)$ htt: //www.domain.com/$4
    


    6 - Prevent commenting if referrer is missing.
    The method is based on the fact that many spam bots do not transmit referer when they post data.
    This code checks the referrer field and blocks sending comments if there is no referer when accessing the wp-comments-post.php file.
    Do not forget to enter the domain of your blog in line 4

    Rewriteengine on
    RewriteCond% {REQUEST_METHOD} POST
    RewriteCond% {REQUEST_URI} .wp-comments-post \ .php *
    RewriteCond% {HTTP_REFERER}!. * Yourblog.com. * [OR]
    RewriteCond% {HTTP_USER_AGENT} ^ $
    RewriteRule (. *) ^ Http: //% {REMOTE_ADDR} / $ [R = 301, L]
    


    7 - Redirect the user to the stub page
    while working on the site; it is advisable to redirect users to the temporary stub page
    Replace maintenance.html in line 2 with the name of your file.
    And in line 3, enter your IP so that you are not redirected to this stub.

    The 302 redirect is used to prevent search engines from indexing the contents of a temporary page.

    Rewriteengine on
    RewriteCond% {REQUEST_URI}! /Maintenance.html$
    RewriteCond% {REMOTE_ADDR}! ^ 123 \ .123 \ .123 \ .123
    RewriteRule $ /maintenance.html [R = 302, L]
    


    8 - Protecting your blog from hotlinks
    Hotlik is the use of files placed on your site on the pages of other sites in order to save your server traffic.
    To combat this scourge, the following lines in .htaccess will help

    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]
    


    9 - Allow access to wp-admin only from your IP.
    An additional protection for your blog from hacking is the restriction of the list of addresses from which it is allowed to enter the admin panel of the blog.
    Do not forget to insert your IP in line 8.
    If you want to use additional addresses for access, add the lines allow from xx.xx.xxx.xx

    AuthUserFile / dev / null
    AuthGroupFile / dev / null
    AuthName "Example Access Control"
    AuthType Basic
    
    order deny, allow
    deny from all
    allow from xx.xx.xx.xx
    


    10 - Blocking spammers in WordPress via .htaccess
    Often spam bots come from the same IP. The following trick will help block access from these addresses.
    Enter the spammer's address in line 3.
    You can expand the list of blocked addresses by adding the lines deny from xxx.xx.xxx.xxx.

    
    order allow, deny
    deny from 200.49.176.139
    allow from all
    


    UPD: The original article can be found here . I came across by chance, but could not ignore such utilities, and it turned out what happened - this article.

    Also popular now: