Currently Viewing Posts Tagged htaccess

Fix some Crawl errors of my blog

I found some errors in Google Search Console —  Crawl Error. More than 400 errors of Not-Found.

After checking the details of these errors, I am noticed that they are the dead links which were brought after I converted this blog from MovableType to WordPress.

Some links are changed and I forgot to implement the redirection.

OK. Now I add some rules in .htaccess file to make these dead link to redirect to the correct URLs.

301_htaccess_redirect

Here is some examples.

Monthly archives:

Old url is https://www.yinfor.com/archives/2012/09/

New url is https://www.yinfor.com/2012/09/

So I added following rule into .htaccess file in root.

# Redirect monthly archives
RewriteRule ^archives/([0-9\/]*)/ /$1/ [R=301,L]

Tag page:

Old tag page is https://www.yinfor.com/mtcgi/mt-search.cgi?IncludeBlugs=4,1&tag=beta&limit=20

New tag page is https://www.yinfor.com/tag/beta/

I add following rule into .htaccess

RewriteCond %{QUERY_STRING} ^IncludeBlogs=([0-9,]*)&tag=(.*)&limit=20$
RewriteRule ^(.*)$ https://www.yinfor.com/tag/%2? [L,R=301]

Another type of tag page:

Old tag page is https://www.yinfor.com/mtcgi/mt-search.cgi?tag=Gogle&blog_id=5

New tag page is https://www.yinfor.com/tag/Google/

Add following into .htaccess file.

RewriteCond %{QUERY_STRING} ^tag=(.*)&blog_id=([0-9])$
RewriteRule ^(.*)$ https://www.yinfor.com/tag/%1? [L,R=301]

How to block the hotlink by referrer

Blocking users or sites that originate from a particular domain is a very useful trick of .htaccess. Lets say you check your logs one day, and see tons of referrals from a particular site, yet upon inspection you can’t find a single visible link to your site on theirs. The referral isn’t a “legitimate” one, with the site most likely hot linking to certain files on your site such as images, .css files,  or files you can’t even make out. Especially those mp3 audio or mp4 video files. They are large files. The site hot link your content will eat a lot of portion of your bandwidth, CPU time. The result is make the normal user of your site loading page slower than normal.

Remember, your logs will generate a referrer entry for any kind of reference to your site that has a traceable origin.

Here I have some example of how to write the .htaccess file for Apache Web Server.

RewriteEngine On
RewriteCond %{HTTP_REFERER} .*mp3stahuj.cz.*$ [OR]
RewriteCond %{HTTP_REFERER} .*gdd.ro.*$ [OR]
RewriteCond %{HTTP_REFERER} .*profil.lide.cz.*$ [OR]
RewriteCond %{HTTP_REFERER} .*yahoo.com.*$
RewriteRule ^(.*)$ -

Blocked referrers will be shown a ‘403 Forbidden’ error message.

How to enable php5 at 1and1

With 1&1 Linux hosting packages you have the choice between PHP4, PHP5 and PHP6. 1&1 Web Hosting packages use PHP4 as default for all .php files.
Why 1and1 still using php4 as default? It is too old. A lot of program are not support php4 any more. Just like phpBB 3.
You may choose to simply name the script with the .php5 file extension if there are only a few scripts that require PHP5. If you don’t want to rename all your scripts to .php5, you can add the following into .htaccess file.

AddType x-mapp-php5 .php

Save the text file as .htaccess in the folder where you need to use PHP5 (or upload the file to the correct folder). The settings specified in the .htaccess file are recursive meaning that the settings will apply to the current folder where the file is saved as well as all subfolders.

Why .htaccess does not work

Last Friday, I put an .htaccess file into one folder of my site. The function of this htaccess is to make it auto redirect to other path.
The problem is it is not working. I do have use this kind of code many times. Why it does not work any more?
It looks the Apache works like there is no this .htaccess file here. I think it must be something wrong in my httpd.conf setting.
After double checking with Apache tutorial, I found the answer.

Continue reading “Why .htaccess does not work”

How to stop access by country IP blocks

You may be tired of deleting spams or spammer account from your site. Sometimes your site is in the list of spammer. And also most of time the spammer are coming from the following top 10 countries.
1. Korea
2. China
3. India
4. Russia
5. Turkey
6. Viet Nam
7. Ukraine
8. Brazil
9. Venezuela
10. Pakistan
How to stop them?

Continue reading “How to stop access by country IP blocks”

How to increase Yslow score from C to B at Dreamhost

I have a small site hosted on Dreamhost.
Yslow Score with default setting is C, or 77.
Later I do the following optimization to increase it to B, or 89. Based on Yslow v2 test.
The key is to change .htaccess file.
Put following content into .htaccess file on root of the site.

Continue reading “How to increase Yslow score from C to B at Dreamhost”

Run php from HTML file

The scenario is I have a blog powered by Movabletype. All html files. Later I need it to be running like a php file. Then I can add php codes inside without changing extension name from html to php.
I’d like to keep the URL. The blog is about five years old.
OK.
Two years ago, I add the following into .htaccess file, which is from Let php engine handle html as php.
RemoveHandler .html .htm
AddType application/x-httpd-php .php .htm .html

It works very well until last week.
I got the error like ask me save the file or not. It is not return the correct php or html file.

Continue reading “Run php from HTML file”

Making site stats accessible with htaccess

Dreamhost has a site stats facility included.
But sometimes it is not accessible because of the .htaccess file.
Say WordPress or some customized .htaccess file which will change the rewrite rule.
To make it works, add following code in top of the .htaccess file.

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/(stats|failed_auth\.html).*$[NC]
RewriteRule . - [L]

Assets Directory of MovableType

There is a folder under the MovableType installation. It is used to store the files uploaded.
The default is assets_c.
Because of I manually setup the .htaccess file to convert all underscore(_) to dash(-). The link or file under assets_c will be redir to assets-c. And got the 404 error.
So I changed Assets Directory.
1) Make a directory assets-c.
2) Move all files from assets_c to assets-c.
3) Add one line to mt-config.cgi
AssetCacheDir /assets-c
4) Login the MT system and update all images, etc.
5) Rebuild it.

  • Archives