分享

写回答

发帖

[经验] linux主机.htaccess文件同时设置301跳转和子目录伪静态的经验

GoDaddy GoDaddy 6374 人阅读 | 3 人回复

发表于 2011-4-15 20:59:42 | 显示全部楼层 |阅读模式

今天搞定了我站的.htaccess,实现了如下功能,看代码吧,有讲解
<Files .htaccess>
order allow,deny
deny from all
</Files>
#上面的代码保护.htaccess不被下载

#网站整体301跳转开始
RewriteEngine On
RewriteBase /
rewritecond %{http_host} ^neegame.com [NC]
rewriterule ^(.*)$ http://www.neegame.com/$1 [R=301,NC,L]
#301跳转结束,所有neegame.com的网址一律跳转到www.neegame.com,域名之后的目录、文件名、入参全部不会变

RewriteRule ^([0-9]+)/index\.html$ /error.php?id=$1   #错误页提示,error.php以参数来区分显示不同的错误提示内容

#discuz子目录下的discuz x1.5开始,最后再解释为什么这么写
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^discuz/topic-(.+)\.html$ discuz/portal.php?mod=topic&topic=$1&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^discuz/article-([0-9]+)-([0-9]+)\.html$ discuz/portal.php?mod=view&aid=$1&page=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^discuz/forum-([0-9]+)-([0-9]+)\.html$ discuz/forum.php?mod=forumdisplay&fid=$1&page=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^discuz/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ discuz/forum.php?mod=viewthread&tid=$1&extra=page\%3D$3&page=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^discuz/group-([0-9]+)-([0-9]+)\.html$ discuz/forum.php?mod=group&fid=$1&page=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^discuz/space-(username|uid)-(.+)\.html$ discuz/home.php?mod=space&$1=$2&%1
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^discuz/([a-z]+)-(.+)\.html$ discuz/$1.php?rewrite=$2&%1
#以上discuz x1.5伪静态代码为discuz官方提供,仅修改了目录名

#错误页定义开始
ErrorDocument 400 http://www.neegame.com/400/index.html
ErrorDocument 401 http://www.neegame.com/401/index.html
ErrorDocument 402 http://www.neegame.com/402/index.html
ErrorDocument 403 http://www.neegame.com/403/index.html
ErrorDocument 404 http://www.neegame.com/404/index.html
ErrorDocument 405 http://www.neegame.com/405/index.html
ErrorDocument 406 http://www.neegame.com/406/index.html
ErrorDocument 407 http://www.neegame.com/407/index.html
ErrorDocument 408 http://www.neegame.com/408/index.html
ErrorDocument 409 http://www.neegame.com/409/index.html
ErrorDocument 410 http://www.neegame.com/410/index.html
ErrorDocument 411 http://www.neegame.com/411/index.html
ErrorDocument 412 http://www.neegame.com/412/index.html
ErrorDocument 413 http://www.neegame.com/413/index.html
ErrorDocument 414 http://www.neegame.com/414/index.html
ErrorDocument 500 http://www.neegame.com/500/index.html
ErrorDocument 501 http://www.neegame.com/501/index.html
ErrorDocument 502 http://www.neegame.com/502/index.html
ErrorDocument 503 http://www.neegame.com/503/index.html
ErrorDocument 504 http://www.neegame.com/504/index.html
ErrorDocument 505 http://www.neegame.com/505/index.html
#错误页定义结束,注意的要点:http://www.neegame.com/505/index.html一定要以http://完整网址的形式出现,否则会造成虽然显示错误页,但无法跳转到响应网址

Options -Indexes FollowSymLinks  #定义目录无法找到默认主页时不显示文件目录


以上就是我的.htaccess,整个文件上传到根目录,这里解释一下discuz x1.5伪静态+301跳转的问题,相信难住了不少人,301跳转只能写在根目录,而discuz官方提供的伪静态代码要在discuz所在目录,这样就造成了冲突,不论你把discuz的301写在论坛目录,还是写在根目录,都无法实现301跳转,因为论坛目录里面有.htaccess的情况下就会忽略根目录的.htaccess,这里我是这样写的:

RewriteRule ^discuz/([a-z]+)-(.+)\.html$ discuz/$1.php?rewrite=$2&%1

红色代码部分为论坛的目录,这样就实现了.htaccess文件在根目录,却可以控制论坛目录的伪静态,我相信大部分使用其他系统,如帝国,DEDE,WORDPRESS的人都会遇到这个问题,使用这个方法就可以同时设置伪静态和301跳转了。

哈哈,搞了三天终于搞定了,把经验拿出来跟大家分享下。

把我的站都拿出来晒一下,大家多提意见哈

爱来商城  http://www.35926.com
减肥评选榜  http://www.jfpxb.com   这个正在改版,准备用topapi重写一下
唐人街游戏  http://www.neegame.com    http://www.chinatowngame.net
Free IT Ask  http://www.freeitask.com   下一步就要做这个了,要是谁看上这域名可以联系我哈

[ 本帖最后由 zcwamy 于 2011-4-15 09:05 PM 编辑 ]

回答|共 3 个

zcwamy

发表于 2011-4-15 22:15:29 | 显示全部楼层

申请加精啊,搞这个期间查了无数资料,都没有这方面的内容,原创啦,哈哈

wangchao008

发表于 2011-4-16 01:52:10 | 显示全部楼层

学习了.

lwbo1987

发表于 2011-4-16 09:11:56 | 显示全部楼层

的确不错!
您需要登录后才可以回帖 登录 | 注册

本版积分规则