.htaccess를 사용하여 도메인을 하위 디렉터리로 리디렉션시키기

.htaccess를 사용하여 메인 도메인 URL을 하위 디렉터리로 리디렉션시킬 수 있습니다.

예를 들어, 워드프레스(WordPress)를 하위 디렉터리에 설치한 경우 메인 도메인 주소를 입력하면 워드프레스가 설치된 하위 디렉터리로 이동시킬 때 사용할 수 있습니다. (워드프레스의 경우 이 방법보다 "워드프레스 주소 변경으로 사이트에 접속하지 못하는 문제 해결" 글에서 "워드프레스 사이트 주소를 하위 폴더에서 루트로 변경하기" 부분을 참고하면 더 깔끔하게 문제를 해결할 수 있습니다. 하지만 경우에 따라 이 방법도 유용할 수 있습니다.)

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?YourDomain.com$
RewriteRule ^(/)?$ blog [L]
# Source: site5 Blog

YourDomain.com을 도메인 메인 주소로 바꾸고 , blog를 실제 하위 디렉터리로 바꾸도록 합니다.

RewriteEngine On

# Map http://www.YourDomain.com /blog.
RewriteRule ^$ /blog/ [L]

# Map http://www.YourDomain.com/x to /blog/x unless there is a x in the web root.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/blog/
RewriteRule ^(.*)$ /blog/$1

# Add trailing slash to directories within blog
# This does not expose the internal URL.
RewriteCond %{SCRIPT_FILENAME} -d
RewriteRule ^blog/(.*[^/])$ http://www.YourDomain.com/$1/ [R=301]
# Source: stackoverflow

마찬가지로 메인 도메인 URL을 입력하면 /blog 하위 디렉터리로 자동으로 이동하게 됩니다.

# .htaccess main domain to subdirectory 'blog' redirect
# Do not change this line.
RewriteEngine on
# Change YourDomain.com to be your main domain.
RewriteCond %{HTTP_HOST} ^(www.)?YourDomain.com$
# Change 'blog' to be the directory you will use for your main domain.
RewriteCond %{REQUEST_URI} !^/blog/
# Don't change the following two lines.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Change 'blog' to be the directory you will use for your main domain.
RewriteRule ^(.*)$ /blog/$1
# Change YourDomain.com to be your main domain again.
# Change 'blog' to be the directory you will use for your main domain
# followed by / then the main file for your site, index.php, index.html, etc.
RewriteCond %{HTTP_HOST} ^(www.)?YourDomain.com$
#RewriteRule ^(/)?$ blog/index.html [L]
## index.php for WordPress
RewriteRule ^(/)?$ blog/index.php [L]
# Source: stackexchange

편의상 메인 도메인 주소는 YourDomain.com으로, 하위 디렉터리는 blog로 통일했습니다. 이 코드는 각 라인마다 자세한 설명이 있어 코드를 더 잘 이해할 수 있습니다. 이 코드를 적용해보면 www.example.com/blog/abc 대신 www.example.com/abc 형식으로 하위 디렉터리 없이 입력해도 정상적으로 작동합니다. 즉, 이 경우 브라우저 URL에 하위 디렉터리(blog)가 표시되지 않으면서 정상적으로 페이지가 표시됩니다. (워드프레스를 하위 폴더에 설치했을 때 이 방법을 이용하면, 굳이 이 글에 설명된 방법을 따르지 않더라도 하위 디렉터리를 효과적으로 숨길 수 있습니다.)

참고:

일부 글에 제휴 링크가 포함될 수 있으며 파트너스 활동으로 일정액의 수수료를 받을 수 있습니다.

댓글 남기기

* 이메일 정보는 공개되지 않습니다.