Dev Blog

스크립트 & 소스코드 공유

실무에서 유용하게 쓰는 스크립트와 코드 조각을 블로그 게시판으로 저장하고 공유합니다.

2건의 글

도메인 차단·이전 시 301 리다이렉트로 강제 이동시키기

도메인이 차단되거나 새 주소로 이전할 때 301 리다이렉트로 방문자를 강제로 이동시키는 방법이다. PHP의 header() 함수, ASP와 ASP.NET, CGI Perl, JSP 코드, Apache .htaccess의 mod_rewrite 규칙, 그리고 IIS의 영구 리다이렉트 설정까지 서버별 구현 방법을 모두 정리했다.

域名跳转可以使用301跳转。301跳转多用于旧网址在废弃前转向新网址以保证用户的访问,在诸多服务器中,均支持本跳转方法。

301跳转共有两种方法:

1、服务器设置法:本设置方法因服务器软件版本较多,且设置繁琐,不推荐使用。

2、页面跳转方法,本方法在浏览器中执行,大多数浏览器均支持本301跳转页面,所以使用较为广泛。

详细方法:

1、IIS下301设置:

Internet信息服务管理器 -> 虚拟目录 -> 重定向到URL,输入需要转向的目标URL,并选择“资源的永久重定向”。

2、PHP转向版本,请使用以下代码:

header("HTTP/1.1 301 Moved Permanently");

header("Location: http://你的网址/");

exit();

3、ASP转向版本,请使用以下代码:

Response.Status="301 Moved Permanently"

Response.AddHeader "Location","http://你的网址/"

Response.End

4、ASP.net转向版本,请使用以下代码:

<script runat=”server”>

private void Page_Load(object sender, System.EventArgs e)

{

Response.Status = “301 Moved Permanently”;

Response.AddHeader(”Location”,”http://www.ahwebs.com/articles/301/“);;)

}

</script>

5、CGI Perl下的301转向,请使用以下代码:

$q = new CGI;

print $q->redirect(”http://www.new-url.com/”);;)

6、JSP转向版本,请使用以下代码:

<%

response.setStatus(301);

response.setHeader( “Location”, “http://www.ahwebs.com/” );

response.setHeader( “Connection”, “close” );

%>

7、Apache下301转向,请使用以下方法:

新建.htaccess文件,输入下列内容(需要开启mod_rewrite):

1)将不带WWW的域名转向到带WWW的域名下

Options +FollowSymLinks

RewriteEngine on

RewriteCond %{HTTP_HOST} ^lesishu.cn [NC]

RewriteRule ^(.*)$ http://www.ahwebs.com/$1 [L,R=301]

2)重定向到新域名

Options +FollowSymLinks

RewriteEngine on

RewriteRule ^(.*)$ http://www.ahwebs.com/$1 [L,R=301]

3)使用正则进行301转向,实现伪静态

Options +FollowSymLinks

RewriteEngine on

RewriteRule ^news-(.+).html$ news.php?id=$1

将news.php?id=123这样的地址转向到news-123.html

그누보드5 짧은주소(rewrite) 사용하기 - Apache 설정

그누보드5의 짧은 주소 기능을 Apache 서버에서 사용하기 위한 설정 방법이다. httpd -M 명령으로 rewrite_module 활성화 여부를 확인하고, 비활성 상태라면 00-base.conf에 LoadModule rewrite_module modules/mod_rewrite.so를 추가한다. 이후 그누보드 환경설정에서 제공하는 rewrite 규칙을 .htaccess에 붙여넣어 사용한다.

Apache 서버인 경우 rewrite_module 이 비활성화 되어 있으면 짧은 주소를 사용할수 없습니다.

위 멘트에 대한 활성화 설정방법입니다.

[root@eyedino www]# httpd -M

------------생략---------------------

negotiation_module (shared)

remoteip_module (shared)

reqtimeout_module (shared)

request_module (shared)

rewrite_module (shared) //있으면 활성된 것이고 없으면 다음단계 진입

setenvif_module (shared)

slotmem_plain_module (shared)

slotmem_shm_module (shared)

socache_dbm_module (shared)

socache_memcache_module (shared)

-------------생략------------------------

다음단계

[root@eyedino www]# nano /etc/httpd/conf.modules.d/00-base.conf

또는 vi editor

[root@eyedino www]# vi /etc/httpd/conf.modules.d/00-base.conf

마지막줄에 붙여넣기

LoadModule rewrite_module modules/mod_rewrite.so

하시면 기본설정은 끝납니다.

.htaccess 파일설정

아시는 것처럼 루트 홈폴더 에

그누보드 환경설정/짧은주소/apache 설정 코드 보기를 복사하여 붙여넣기합니다.

그누보드5 rewrite BEGIN

<IfModule mod_rewrite.c>

RewriteEngine On

RewriteBase /

RewriteCond %{REQUEST_FILENAME} -f [OR]

RewriteCond %{REQUEST_FILENAME} -d

RewriteRule ^ - [L]

RewriteRule ^shop/list-([0-9a-z]+)$ shop/list.php?ca_id=$1&rewrite=1 [QSA,L]

RewriteRule ^shop/type-([0-9a-z]+)$ shop/listtype.php?type=$1&rewrite=1 [QSA,L]

RewriteRule ^shop/([0-9a-zA-Z_]+)$ shop/item.php?it_id=$1&rewrite=1 [QSA,L]

RewriteRule ^shop/([^/]+)/$ shop/item.php?it_seo_title=$1&rewrite=1 [QSA,L]

RewriteRule ^content/([0-9a-zA-Z_]+)$ bbs/content.php?co_id=$1&rewrite=1 [QSA,L]

RewriteRule ^content/([^/]+)/$ bbs/content.php?co_seo_title=$1&rewrite=1 [QSA,L]

RewriteRule ^rss/([0-9a-zA-Z_]+)$ bbs/rss.php?bo_table=$1 [QSA,L]

RewriteRule ^([0-9a-zA-Z_]+)$ bbs/board.php?bo_table=$1&rewrite=1 [QSA,L]

RewriteRule ^([0-9a-zA-Z_]+)/([^/]+)/$ bbs/board.php?bo_table=$1&wr_seo_title=$2&rewrite=1 [QSA,L]

RewriteRule ^([0-9a-zA-Z_]+)/write$ bbs/write.php?bo_table=$1&rewrite=1 [QSA,L]

RewriteRule ^([0-9a-zA-Z_]+)/([0-9]+)$ bbs/board.php?bo_table=$1&wr_id=$2&rewrite=1 [QSA,L]

</IfModule>

그누보드5 rewrite END

.htaccess파일을 저장합니다.

(혹시 root 소유권 으로 실행 안될 수도 있으니 소유권도 변경 바랍니다.

[root@eyedino www]# chown eyedino.eyedino .htaccess

마지막 단계

아파치 설정파일 설정

[root@eyedino www]# nano /etc/httpd/conf/httpd.conf

또는

[root@eyedino www]# vi nano /etc/httpd/conf/httpd.conf

<Directory "/home/*****/www">

AllowOverride None -> ALL 바꿈

Allow open access:

Require all granted

</Directory>

<Directory "/home/*****/www">

AllowOverride ALL

Allow open access:

Require all granted

</Directory>

CETOS8 에서는 기본값으로

-------------생략-----------------

The following lines prevent .htaccess and .htpasswd files from being

viewed by Web clients.

<Files ".ht*">

Require all denied

</Files>

-----------생략-------------------

이부분 막아놨더군요!

<Files ".ht*">

Require all denied

</Files>

주석정리 해주시고

httpd 재시작하시면 끝~

[root@eyedino www]# systemctl restart httpd

도움이 되었으면 좋겠습니다.



Let's create something great together.

프로젝트 문의 및 견적 요청은 언제든 환영합니다.