2014-08-29 2 views
1

완전히 새로운 웹 사이트 (subdomain.domain.com = subdomain.com)로 작동하는 하위 도메인이 있습니다. 나는 주로 .html 파일을 사용하지만 그 확장자를 없애고 싶습니다 (http://subdomain.com/file.html 대신, http://subdomain.com/file). 나는 이것을 달성하기 위해 아래의 .htaccess 코드를 사용했다..htaccess로 여러 파일 확장명 제거

Options +FollowSymLinks 
RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ $1.html [L,QSA] 

유일한 문제는 웹 사이트에서 다른 파일의 .php 확장자를 제거해야한다는 것입니다. 대부분의 사람들은 .html 대신 .php를 사용하여 위의 코드를 복제한다고 말합니다. 나는 그것을했다. 작동하지 않습니다. 코드의 다른 많은 버전을 시도했지만 여전히 작동하지 않습니다.

나는 .htaccess에 대한 전문 지식이 없으므로 아무도 .html 및 .php 파일 확장명을 모두 제거하는 방법을 설명 할 수 있습니까? 미리 감사드립니다.

다른 사람이 한 번에 여러 파일 확장명을 바꾸는 방법을 묻지 않았으므로 중복되는 질문이라고 생각하지 않습니다.

답변

2

당신은 다음과 같이 재 작성 규칙을 사용할 수 있습니다

Options +FollowSymLinks 
RewriteEngine on 

# for adding .html extension if matching file exists 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{DOCUMENT_ROOT}/$1\.html -f [NC] 
RewriteRule ^(.+?)/?$ /$1.html [L] 

# for adding .php extension if matching file exists 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC] 
RewriteRule ^(.+?)/?$ /$1.php [L] 
+0

답변 해 주셔서 감사합니다. –

+0

파일은 주 도메인 폴더 (하위 도메인 아님)에 있습니다. 그리고 당신의 코드는 다른 규칙이 무엇이든 상관없이 404를 던집니다. 나는 그들을 모두 삭제하고 시도했습니다 ... –

+0

또한 'RewriteRule^(. +?) /? $ $ 1.html [L]') – anubhava

0

여러 개의 파일 확장자를 제거하려면 /root/.htaccess에 다음과 같은 규칙을 사용할 수 있습니다.

RewriteEngine on 

RewriteBase/
#terminate all rewrite loops 
RewriteCond %{ENV:REDIRECT_STATUS} 200 
RewriteRule^- [L] 
#1)Remove ".html" , ".php",".css" extension. 
#The following rule redirects "/file.ext" to "/file" 
RewriteRule ^(.+)\.(html|php|css)$ $1 [L,R,NE] 
#checks if "/file" .html exists, if it exits ,map "/file" to "/file.html" 
RewriteCond %{REQUEST_FILENAME}.html -f 
RewriteRule ^(.*?)/?$ $1.html [L] 
#checks if "/file" .php exists, if it exits ,map "/file" to "/file.php" 
RewriteCond %{REQUEST_FILENAME}.php -f 
RewriteRule ^(.*?)/?$ $1.php [L] 
#checks if "/file" .css exists, if it exits ,map "/file" to "/file.css" 
RewriteCond %{REQUEST_FILENAME}.css -f 
RewriteRule ^(.*?)/?$ $1.css [L] 

위 규칙은 .php, .html 및 .css 확장자를 제거합니다. 더 많은 확장 프로그램을 제거하려면 필요에 따라이 .htaccess 파일을 편집 할 수 있습니다.