IT News

웹서버(web server) rewrite 설정하는 법

skyLove1982 2015. 11. 11. 10:23
반응형

웹서버에 rewrite 설정하는 방법을 가끔씩 잊어 먹기에 여기에 적어놓고자 한다.

rewirte 를 사용하려면 우선은 rewrite mod 가 활성화 되어 있어야 한다. php info 를 통해서 확인이 가능하고 만일 안될 경우에는 검색을 통해 알아보고.. 설정파일 2개에서 무엇을 설정했는지 알아야 한다.

 

첫째는 .htaccess 파일이다. 여기서 rewrite 엔진을 on 시킨다. 그리고 규칙을 적어 놓는다. 그리고 두번째로 httpd.conf 에는 설정에서 AllowOverride All  이 되어야 한다. 단 여기서는 웹 디렉토리가 /var/www/html 이다.

 

 

/var/www/html/.htaccess
----------------------------------------------------------------------------------
RewriteEngine on
# 요청 파일이 디렉터리에서 찾아봐도 없거나 아니고, 파일에서 찾아봐도 없거나 일반파일이 아니어야 한다.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# 그 경우, 확장자를 떼어버리고 .php를 붙여본다.
RewriteRule ^([^.]+) $1.php
==================================================================================

 

./etc/httpd/conf/httpd.conf
----------------------------------------------------------------------------------
<Directory />
    AllowOverride none
    Require all denied
</Directory>

#
# Note that from this point forward you must specifically allow
# particular features to be enabled - so if something's not working as
# you might expect, make sure that you have specifically enabled it
# below.
#

#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "/var/www/html"

#
# Relax access to content within /var/www.
#
<Directory "/var/www">
    AllowOverride None
    # Allow open access:
    Require all granted
</Directory>

# Further relax access to the default document root:
<Directory "/var/www/html">
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.4/mod/core.html#options
    # for more information.
    #
    Options FollowSymLinks

    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   Options FileInfo AuthConfig Limit
    #
    AllowOverride All

    #
    # Controls who can get stuff from this server.
    #
    Require all granted
</Directory>

=================================================================================================

반응형