May
15
1.时区设置
有些时候,当你在PHP里使用date或mktime函数时,由于时区的不同,它会显示出一些很奇怪的信息。下面是解决这个问题的方法之一。就是设置你的服务器的时区。你可以在这里找到所有支持的时区的清单。
SetEnv TZ Australia/Melbourne
2. 搜索引擎友好的301永久转向方法
为什么这是搜索引擎友好的呢?因为现在很多现代的搜索引擎都有能根据检查301永久转向来更新它现有的记录的功能。
Redirect 301 http://www.aqee.net/home http://www.aqee.net/
3. 屏蔽下载对话框
通常,当你下载东西的时候,你会看到一个对话框询问你是保持这个文件还是直接打开它。如果你不想看到这个东西,你可以把下面的一段代码放到你的.htaccess文件里。
AddType application/octet-stream .pdf
AddType application/octet-stream .zip
AddType application/octet-stream .mov
4. 省去www前缀
SEO的一个原则是,确保你的网站只有一个URL。因此,你需要把所有的通过www的访问转向的非www,或者反这来。
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.aqee.net [NC]
RewriteRule ^(.*)$ http://aqee.net/$1 [L,R=301]
5. 个性化Error页面
对每个错误代码定制自己个性化的错误页面。
ErrorDocument 401 /error/401.php
ErrorDocument 403 /error/403.php
ErrorDocument 404 /error/404.php
ErrorDocument 500 /error/500.php
6. 压缩文件
通过压缩你的文件体积来优化网站的访问速度。
# 压缩 text, html, javascript, css, xml:
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
7. 缓存文件
缓存文件是另外一个提高你的网站访问速度的好方法。
<FilesMatch “.(flv|gif|jpg|jpeg|png|ico|swf|js|css|pdf)$”>
Header set Cache-Control “max-age=2592000″
</FilesMatch>
8. 对某些文件类型禁止使用缓存
而另一方面,你也可以定制对某些文件类型禁止使用缓存。
# 显式的规定对脚本和其它动态文件禁止使用缓存
<FilesMatch “.(pl|php|cgi|spl|scgi|fcgi)$”>
Header unset Cache-Control
</FilesMatch>
安全问题
下面的htaccess代码能够提高你的web服务器的安全水平。图片链接盗用保护非常有用,它能防止其他人偷盗使用你的服务器上的图片资源。
1. 通过.htaccess放盗链
痛恨那些偷盗链接你的web服务器上的图片资源而耗尽了你的带宽的行为吗?试试这个,你可以防止这种事情的发生。
RewriteBase /
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www.)?aqee.net/.*$ [NC]
RewriteRule .(gif|jpg|swf|flv|png)$ /feed/ [R=302,L]
2. 防黑客
如果你想提高网站的安全等级,你可以去掉下面的几行代码,这样可以防止一些常见恶意URL匹配的黑客攻击技术。
RewriteEngine On
# proc/self/environ? 没门!
RewriteCond %{QUERY_STRING} proc/self/environ [OR]
# 阻止脚本企图通过URL修改mosConfig值
RewriteCond %{QUERY_STRING} mosConfig_[a-zA-Z_]{1,21}(=|%3D) [OR]
# 阻止脚本通过URL传递的base64_encode垃圾信息
RewriteCond %{QUERY_STRING} base64_encode.*(.*) [OR]
# 阻止在URL含有<script>标记的脚本
RewriteCond %{QUERY_STRING} (<|%3C).*script.*(>|%3E) [NC,OR]
# 阻止企图通过URL设置PHP的GLOBALS变量的脚本
RewriteCond %{QUERY_STRING} GLOBALS(=|[|%[0-9A-Z]{0,2}) [OR]
# 阻止企图通过URL设置PHP的_REQUEST变量的脚本
RewriteCond %{QUERY_STRING} _REQUEST(=|[|%[0-9A-Z]{0,2})
# 把所有被阻止的请求转向到403禁止提示页面!
RewriteRule ^(.*)$ index.php [F,L]
3. 阻止访问你的 .htaccess 文件
下面的代码可以阻止别人访问你的.htaccess文件。同样,你也可以设定阻止多种文件类型。
# 保护你的 htaccess 文件
<Files .htaccess>
order allow,deny
deny from all
</Files>
# 阻止查看指定的文件
<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>
4. 重命名 htaccess 文件
你可以通过重命名htaccess文件来对其进行保护。
AccessFileName htacc.ess
5. 禁止目录浏览
禁止服务器对外显示目录结构,反之亦然。
# 禁止目录浏览
Options All -Indexes
# 开放目录浏览
Options All +Indexes
6. 改变缺省的Index页面
你可以把缺省的 index.html, index.php 或 index.htm 改成其它页面。
DirectoryIndex business.html
7. 通过引用信息来阻止某些不欢迎的浏览者
# 阻止来自某网站的用户
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_REFERER} scumbag.com [NC,OR]
RewriteCond %{HTTP_REFERER} wormhole.com [NC,OR]
RewriteRule .* - [F]
</ifModule>
8. 通过判断浏览器头信息来阻止某些请求
这个方法可以通过阻止某些机器人或蜘蛛爬虫抓取你的网站来节省你的带宽流量。
# 阻止来自某些特定网站的用户
<IfModule mod_rewrite.c>
SetEnvIfNoCase ^User-Agent$ .*(craftbot|download|extract|stripper|sucker|ninja|clshttp|webspider
|leacher|collector|grabber|webpictures) HTTP_SAFE_BADBOT
SetEnvIfNoCase ^User-Agent$ .*(libwww-perl|aesop_com_spiderman) HTTP_SAFE_BADBOT
Deny from env=HTTP_SAFE_BADBOT
</ifModule>
9. 禁止脚本执行,加强你的目录安全
# 禁止某些目录里的脚本执行权限
AddHandler cgi-script .php .pl .py .jsp .asp .htm .shtml .sh .cgi
Options -ExecCGI
有些时候,当你在PHP里使用date或mktime函数时,由于时区的不同,它会显示出一些很奇怪的信息。下面是解决这个问题的方法之一。就是设置你的服务器的时区。你可以在这里找到所有支持的时区的清单。
SetEnv TZ Australia/Melbourne
2. 搜索引擎友好的301永久转向方法
为什么这是搜索引擎友好的呢?因为现在很多现代的搜索引擎都有能根据检查301永久转向来更新它现有的记录的功能。
Redirect 301 http://www.aqee.net/home http://www.aqee.net/
3. 屏蔽下载对话框
通常,当你下载东西的时候,你会看到一个对话框询问你是保持这个文件还是直接打开它。如果你不想看到这个东西,你可以把下面的一段代码放到你的.htaccess文件里。
AddType application/octet-stream .pdf
AddType application/octet-stream .zip
AddType application/octet-stream .mov
4. 省去www前缀
SEO的一个原则是,确保你的网站只有一个URL。因此,你需要把所有的通过www的访问转向的非www,或者反这来。
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.aqee.net [NC]
RewriteRule ^(.*)$ http://aqee.net/$1 [L,R=301]
5. 个性化Error页面
对每个错误代码定制自己个性化的错误页面。
ErrorDocument 401 /error/401.php
ErrorDocument 403 /error/403.php
ErrorDocument 404 /error/404.php
ErrorDocument 500 /error/500.php
6. 压缩文件
通过压缩你的文件体积来优化网站的访问速度。
# 压缩 text, html, javascript, css, xml:
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
7. 缓存文件
缓存文件是另外一个提高你的网站访问速度的好方法。
<FilesMatch “.(flv|gif|jpg|jpeg|png|ico|swf|js|css|pdf)$”>
Header set Cache-Control “max-age=2592000″
</FilesMatch>
8. 对某些文件类型禁止使用缓存
而另一方面,你也可以定制对某些文件类型禁止使用缓存。
# 显式的规定对脚本和其它动态文件禁止使用缓存
<FilesMatch “.(pl|php|cgi|spl|scgi|fcgi)$”>
Header unset Cache-Control
</FilesMatch>
安全问题
下面的htaccess代码能够提高你的web服务器的安全水平。图片链接盗用保护非常有用,它能防止其他人偷盗使用你的服务器上的图片资源。
1. 通过.htaccess放盗链
痛恨那些偷盗链接你的web服务器上的图片资源而耗尽了你的带宽的行为吗?试试这个,你可以防止这种事情的发生。
RewriteBase /
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www.)?aqee.net/.*$ [NC]
RewriteRule .(gif|jpg|swf|flv|png)$ /feed/ [R=302,L]
2. 防黑客
如果你想提高网站的安全等级,你可以去掉下面的几行代码,这样可以防止一些常见恶意URL匹配的黑客攻击技术。
RewriteEngine On
# proc/self/environ? 没门!
RewriteCond %{QUERY_STRING} proc/self/environ [OR]
# 阻止脚本企图通过URL修改mosConfig值
RewriteCond %{QUERY_STRING} mosConfig_[a-zA-Z_]{1,21}(=|%3D) [OR]
# 阻止脚本通过URL传递的base64_encode垃圾信息
RewriteCond %{QUERY_STRING} base64_encode.*(.*) [OR]
# 阻止在URL含有<script>标记的脚本
RewriteCond %{QUERY_STRING} (<|%3C).*script.*(>|%3E) [NC,OR]
# 阻止企图通过URL设置PHP的GLOBALS变量的脚本
RewriteCond %{QUERY_STRING} GLOBALS(=|[|%[0-9A-Z]{0,2}) [OR]
# 阻止企图通过URL设置PHP的_REQUEST变量的脚本
RewriteCond %{QUERY_STRING} _REQUEST(=|[|%[0-9A-Z]{0,2})
# 把所有被阻止的请求转向到403禁止提示页面!
RewriteRule ^(.*)$ index.php [F,L]
3. 阻止访问你的 .htaccess 文件
下面的代码可以阻止别人访问你的.htaccess文件。同样,你也可以设定阻止多种文件类型。
# 保护你的 htaccess 文件
<Files .htaccess>
order allow,deny
deny from all
</Files>
# 阻止查看指定的文件
<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>
4. 重命名 htaccess 文件
你可以通过重命名htaccess文件来对其进行保护。
AccessFileName htacc.ess
5. 禁止目录浏览
禁止服务器对外显示目录结构,反之亦然。
# 禁止目录浏览
Options All -Indexes
# 开放目录浏览
Options All +Indexes
6. 改变缺省的Index页面
你可以把缺省的 index.html, index.php 或 index.htm 改成其它页面。
DirectoryIndex business.html
7. 通过引用信息来阻止某些不欢迎的浏览者
# 阻止来自某网站的用户
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_REFERER} scumbag.com [NC,OR]
RewriteCond %{HTTP_REFERER} wormhole.com [NC,OR]
RewriteRule .* - [F]
</ifModule>
8. 通过判断浏览器头信息来阻止某些请求
这个方法可以通过阻止某些机器人或蜘蛛爬虫抓取你的网站来节省你的带宽流量。
# 阻止来自某些特定网站的用户
<IfModule mod_rewrite.c>
SetEnvIfNoCase ^User-Agent$ .*(craftbot|download|extract|stripper|sucker|ninja|clshttp|webspider
|leacher|collector|grabber|webpictures) HTTP_SAFE_BADBOT
SetEnvIfNoCase ^User-Agent$ .*(libwww-perl|aesop_com_spiderman) HTTP_SAFE_BADBOT
Deny from env=HTTP_SAFE_BADBOT
</ifModule>
9. 禁止脚本执行,加强你的目录安全
# 禁止某些目录里的脚本执行权限
AddHandler cgi-script .php .pl .py .jsp .asp .htm .shtml .sh .cgi
Options -ExecCGI
May
15
1、防盗链
Options +FollowSymlinks
#Protect against hotlinking
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www.)?domainname.com/ [nc]
RewriteRule .*.(gif|jpg|png)$ http://domainname.com/img/stop_stealing_bandwidth.gif[nc]
2、通过User Agents 禁止访问
#Block bad bots
SetEnvIfNoCase user-Agent ^FrontPage [NC,OR]
SetEnvIfNoCase user-Agent ^Java.* [NC,OR]
SetEnvIfNoCase user-Agent ^Microsoft.URL [NC,OR]
SetEnvIfNoCase user-Agent ^MSFrontPage [NC,OR]
SetEnvIfNoCase user-Agent ^Offline.Explorer [NC,OR]
SetEnvIfNoCase user-Agent ^[Ww]eb[Bb]andit [NC,OR]
SetEnvIfNoCase user-Agent ^Zeus [NC]
<limit get="" post="" head="">
Order Allow,Deny
Allow from all
Deny from env=bad_bot
</limit>
3、重定向(禁止)所有访问,某些来源IP除外
ErrorDocument 403 http://www.domainname.com
Order deny,allow
Deny from all
Allow from 124.34.48.165
Allow from 102.54.68.123
4、SEO 友好 301 重定向
Redirect 301 /d/file.html http://www.domainname.com/r/file.html
5、自定义错误页面
ErrorDocument 401 /error/401.php
ErrorDocument 403 /error/403.php
ErrorDocument 404 /error/404.php
ErrorDocument 500 /error/500.php
6、禁止某些来源IP
allow from all
deny from 145.186.14.122
deny from 124.15
7、设置服务器管理员的默认邮箱地址
ServerSignature EMail
SetEnv SERVER_ADMIN [email protected]
8、禁用显示下载请求
AddType application/octet-stream .pdf
AddType application/octet-stream .zip
AddType application/octet-stream .mov
9、保护特定的文件,禁止访问。
#Protect the .htaccess File
<files .htaccess="">
order allow,deny
deny from all
</files>
10、利用mod_deflate压缩文件
<ifmodule mod_deflate.c="">
<filesmatch .(js|css)$="">
SetOutputFilter DEFLATE
</filesmatch>
</ifmodule>
11、添加Expires头
<filesmatch .(ico|pdf|flv|jpg|jpeg|png|gif|swf)$="">
Header set Expires "Wed, 21 May 2010 20:00:00 GMT"
</filesmatch>
12、设置默认页
#Serve Alternate Default Index Page
DirectoryIndex about.html
13、设置密码保护文件和目录
#password-protect a file
<files secure.php="">
AuthType Basic
AuthName "Prompt"
AuthUserFile /home/path/.htpasswd
Require valid-user
</files>
# password-protect a directory
resides
AuthType basic
AuthName "This directory is protected"
AuthUserFile /home/path/.htpasswd
AuthGroupFile /dev/null
Require valid-user
14、将老域名重定向新域名
#Redirect from an old domain to a new domain
RewriteEngine On
RewriteRule ^(.*)$ http://www.domainname.com/$1 [R=301,L]
15、强制缓存
FileETag MTime Size
ExpiresActive on
ExpiresDefault "access plus 86400 seconds"
16、启用GZIP压缩相关文件
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
17、从URL中移除“category”
RewriteRule ^category/(.+)$ http://www.yourdomain.com/$1 [R=301,L]
18、禁止目录浏览
Options All -Indexes
19、重定向WordPress的订阅到Feedburner
#Redirect wordpress content feeds to feedburner
<ifmodule mod_rewrite.c="">
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} !FeedBurner [NC]
RewriteCond %{HTTP_USER_AGENT} !FeedValidator [NC]
RewriteRule ^feed/?([_0-9a-z-]+)?/?$ http://feeds.feedburner.com/yourfeed [R=302,NC,L]
</ifmodule>
20、禁止空Referrer访问内容
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]
21、从URL中删除文件扩展名
RewriteRule ^(([^/]+/)*[^.]+)$ /$1.php [L]
22、将www.domain.com重定向到domain.com
#remove www from URI
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]
23、URL结尾添加/
#trailing slash enforcement
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !#
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://domain.com/$1/ [L,R=301
24、将www.domain.com/xxx重定向到domain.com/xxx
# Redirect if www.yourdomain.com to yourdomain.com
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule (.*) http://example.com/$1 [R=301,L]
Options +FollowSymlinks
#Protect against hotlinking
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www.)?domainname.com/ [nc]
RewriteRule .*.(gif|jpg|png)$ http://domainname.com/img/stop_stealing_bandwidth.gif[nc]
2、通过User Agents 禁止访问
#Block bad bots
SetEnvIfNoCase user-Agent ^FrontPage [NC,OR]
SetEnvIfNoCase user-Agent ^Java.* [NC,OR]
SetEnvIfNoCase user-Agent ^Microsoft.URL [NC,OR]
SetEnvIfNoCase user-Agent ^MSFrontPage [NC,OR]
SetEnvIfNoCase user-Agent ^Offline.Explorer [NC,OR]
SetEnvIfNoCase user-Agent ^[Ww]eb[Bb]andit [NC,OR]
SetEnvIfNoCase user-Agent ^Zeus [NC]
<limit get="" post="" head="">
Order Allow,Deny
Allow from all
Deny from env=bad_bot
</limit>
3、重定向(禁止)所有访问,某些来源IP除外
ErrorDocument 403 http://www.domainname.com
Order deny,allow
Deny from all
Allow from 124.34.48.165
Allow from 102.54.68.123
4、SEO 友好 301 重定向
Redirect 301 /d/file.html http://www.domainname.com/r/file.html
5、自定义错误页面
ErrorDocument 401 /error/401.php
ErrorDocument 403 /error/403.php
ErrorDocument 404 /error/404.php
ErrorDocument 500 /error/500.php
6、禁止某些来源IP
allow from all
deny from 145.186.14.122
deny from 124.15
7、设置服务器管理员的默认邮箱地址
ServerSignature EMail
SetEnv SERVER_ADMIN [email protected]
8、禁用显示下载请求
AddType application/octet-stream .pdf
AddType application/octet-stream .zip
AddType application/octet-stream .mov
9、保护特定的文件,禁止访问。
#Protect the .htaccess File
<files .htaccess="">
order allow,deny
deny from all
</files>
10、利用mod_deflate压缩文件
<ifmodule mod_deflate.c="">
<filesmatch .(js|css)$="">
SetOutputFilter DEFLATE
</filesmatch>
</ifmodule>
11、添加Expires头
<filesmatch .(ico|pdf|flv|jpg|jpeg|png|gif|swf)$="">
Header set Expires "Wed, 21 May 2010 20:00:00 GMT"
</filesmatch>
12、设置默认页
#Serve Alternate Default Index Page
DirectoryIndex about.html
13、设置密码保护文件和目录
#password-protect a file
<files secure.php="">
AuthType Basic
AuthName "Prompt"
AuthUserFile /home/path/.htpasswd
Require valid-user
</files>
# password-protect a directory
resides
AuthType basic
AuthName "This directory is protected"
AuthUserFile /home/path/.htpasswd
AuthGroupFile /dev/null
Require valid-user
14、将老域名重定向新域名
#Redirect from an old domain to a new domain
RewriteEngine On
RewriteRule ^(.*)$ http://www.domainname.com/$1 [R=301,L]
15、强制缓存
FileETag MTime Size
ExpiresActive on
ExpiresDefault "access plus 86400 seconds"
16、启用GZIP压缩相关文件
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
17、从URL中移除“category”
RewriteRule ^category/(.+)$ http://www.yourdomain.com/$1 [R=301,L]
18、禁止目录浏览
Options All -Indexes
19、重定向WordPress的订阅到Feedburner
#Redirect wordpress content feeds to feedburner
<ifmodule mod_rewrite.c="">
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} !FeedBurner [NC]
RewriteCond %{HTTP_USER_AGENT} !FeedValidator [NC]
RewriteRule ^feed/?([_0-9a-z-]+)?/?$ http://feeds.feedburner.com/yourfeed [R=302,NC,L]
</ifmodule>
20、禁止空Referrer访问内容
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]
21、从URL中删除文件扩展名
RewriteRule ^(([^/]+/)*[^.]+)$ /$1.php [L]
22、将www.domain.com重定向到domain.com
#remove www from URI
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]
23、URL结尾添加/
#trailing slash enforcement
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !#
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://domain.com/$1/ [L,R=301
24、将www.domain.com/xxx重定向到domain.com/xxx
# Redirect if www.yourdomain.com to yourdomain.com
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule (.*) http://example.com/$1 [R=301,L]
May
15
1. Apache模块 mod_alias的 Redirect 和 RedirectMatch命令
上面提到2个命令使用方法相似。而区别就是后者RedirectMatch基于正则表达式匹配对当前的URL发送一个外部重定向语法为:
Redirect [status] URL-path URL
RedirectMatch [status] regex URL
status参数可以使用以下HTTP状态码:
permanent
返回一个永久性重定向状态码(301),表示此资源的位置变动是永久性的。
temp
返回一个临时性重定向状态码(302),这是默认值。
seeother
返回一个“参见”状态码(303),表示此资源已经被替代。
gone
返回一个“已废弃”状态码(410),表示此资源已经被永久性地删除了。如果指定了这个状态码,则URL参数将被忽略。
举例:
APACHE
Redirect 301 /old/old.htm http://www.abc.com/new.htm
Redirect permanent /one http://abc.com/two
RedirectMatch 301 (.*).gif$ http://www.abc.com/images/$1.jpg
2.使用mod_rewrite重写URL方式
APACHE
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^abc.com
RewriteRule ^(.*)$ http://www.abc.com/$1 [R=permanent,L]
在这里判断当前服务器变量HTTP_HOST是否等于abc.com,为真就进行重写,按照R=permanent进行永久重定向,L表示并立即停止重写操作,并不再应用其他重写规则
下面是我最终实现的.htaccess文件,同时也并入wordpress重写规则。
APACHE
# BEGIN WordPress
RewriteEngine On
#Redirect
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^abc.com$
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^(.*)$ http://www.abc.com/$1 [R=301,L]
#Rewrite(blog)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/.* /blog/index.php [L]
RewriteRule . -
# END WordPress
上面提到2个命令使用方法相似。而区别就是后者RedirectMatch基于正则表达式匹配对当前的URL发送一个外部重定向语法为:
Redirect [status] URL-path URL
RedirectMatch [status] regex URL
status参数可以使用以下HTTP状态码:
permanent
返回一个永久性重定向状态码(301),表示此资源的位置变动是永久性的。
temp
返回一个临时性重定向状态码(302),这是默认值。
seeother
返回一个“参见”状态码(303),表示此资源已经被替代。
gone
返回一个“已废弃”状态码(410),表示此资源已经被永久性地删除了。如果指定了这个状态码,则URL参数将被忽略。
举例:
APACHE
引用
Redirect 301 /old/old.htm http://www.abc.com/new.htm
Redirect permanent /one http://abc.com/two
RedirectMatch 301 (.*).gif$ http://www.abc.com/images/$1.jpg
2.使用mod_rewrite重写URL方式
APACHE
引用
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^abc.com
RewriteRule ^(.*)$ http://www.abc.com/$1 [R=permanent,L]
在这里判断当前服务器变量HTTP_HOST是否等于abc.com,为真就进行重写,按照R=permanent进行永久重定向,L表示并立即停止重写操作,并不再应用其他重写规则
下面是我最终实现的.htaccess文件,同时也并入wordpress重写规则。
APACHE
引用
# BEGIN WordPress
RewriteEngine On
#Redirect
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^abc.com$
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^(.*)$ http://www.abc.com/$1 [R=301,L]
#Rewrite(blog)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/.* /blog/index.php [L]
RewriteRule . -
# END WordPress
May
15
一、开始菜单-运行(Win+R)-输入regedit打开注册表编辑器
找到下面这项
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server\Wds\rdpwd\Tds\tcp
这时候你就可以看见右边的PortNumber键值了,双击PortNumber把基数改为十进制,修改成你想要改的端口号。
二、只修改了上面的这一个键值还是不行的,还要接着把下面的这个键值也修改成和上面的一样才可以
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp
这个键值右边有很多选项,找到PortNumber这个键值,双击把基数改为十进制,端口号改成和上面修改的一致,退出注册表编辑器。
三、上面修改好了之后,你还要更新下防火墙的开放端口号,否则服务器重启之后你就连不上服务器了,你可以在修改默认的远程桌面端口号之前就把要设置的端口号在防火墙中排除掉,这样重启之后你就可以用修改后得端口远程连接你的服务器了,如你修改的端口号为7788那么你在远程连接工具的IP地址后面加上:7788就可以了。
找到下面这项
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server\Wds\rdpwd\Tds\tcp
这时候你就可以看见右边的PortNumber键值了,双击PortNumber把基数改为十进制,修改成你想要改的端口号。
二、只修改了上面的这一个键值还是不行的,还要接着把下面的这个键值也修改成和上面的一样才可以
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp
这个键值右边有很多选项,找到PortNumber这个键值,双击把基数改为十进制,端口号改成和上面修改的一致,退出注册表编辑器。
三、上面修改好了之后,你还要更新下防火墙的开放端口号,否则服务器重启之后你就连不上服务器了,你可以在修改默认的远程桌面端口号之前就把要设置的端口号在防火墙中排除掉,这样重启之后你就可以用修改后得端口远程连接你的服务器了,如你修改的端口号为7788那么你在远程连接工具的IP地址后面加上:7788就可以了。