2012-11-09 10 views
1

.htaccess 파일을 사용하여 다음 유형의 다시 쓰기를 수행하는 예를 하루 종일 Google에서 검색했지만 아무 것도 제시하지 않았습니다. "쓰기"규칙을 다시 읽었지 만 쉽게 해결할 수있는 주제가 아닙니다. 어쨌든Apache Rewrite Rules로 URL 다시 쓰기

, 어떻게 나는 다음과 같은 URL을

whatever.com/any/number/or/level/of/directories

을이 URL

http://www.whatever.com/any/number/or/level/of/directories에에 다시 것?

미리 감사드립니다.

편집은 내가 함께 일하고 있어요 웹 사이트에 대해 두 가지를 언급하는 것을 잊었다. 먼저, Drupal 7을 사용하여 설계된 웹 사이트에서 작업하고 있습니다. 모듈 리디렉션이 사용 가능합니다.

EDIT (htaccess로) 다음과 같은

# 
# Apache/PHP/Drupal settings: 
# 

# Protect files and directories from prying eyes. 
<FilesMatch "\.(engine|inc|info|install|make|module|profile|test|po|sh|.*sql|theme|tpl(\.php)?|xtmpl)$|^(\..*|Entries.*|Repository|Root|Tag|Template)$"> 
    Order allow,deny 
</FilesMatch> 

# Don't show directory listings for URLs which map to a directory. 
Options -Indexes 

# Follow symbolic links in this directory. 
Options +FollowSymLinks 

# Make Drupal handle any 404 errors. 
ErrorDocument 404 /index.php 

# Set the default handler. 
DirectoryIndex index.php index.html index.htm 

# Override PHP settings that cannot be changed at runtime. See 
# sites/default/default.settings.php and drupal_environment_initialize() in 
# includes/bootstrap.inc for settings that can be changed at runtime. 

# PHP 5, Apache 1 and 2. 
<IfModule mod_php5.c> 
    php_flag magic_quotes_gpc     off 
    php_flag magic_quotes_sybase    off 
    php_flag register_globals     off 
    php_flag session.auto_start    off 
    php_value mbstring.http_input    pass 
    php_value mbstring.http_output   pass 
    php_value post_max_size     65M 
    php_value upload_max_filesize    65M 
    php_flag mbstring.encoding_translation off 
</IfModule> 

# Requires mod_expires to be enabled. 
<IfModule mod_expires.c> 
    # Enable expirations. 
    ExpiresActive On 

    # Cache all files for 2 weeks after access (A). 
    ExpiresDefault A1209600 

    <FilesMatch \.php$> 
    # Do not allow PHP scripts to be cached unless they explicitly send cache 
    # headers themselves. Otherwise all scripts would have to overwrite the 
    # headers set by mod_expires if they want another caching behavior. This may 
    # fail if an error occurs early in the bootstrap process, and it may cause 
    # problems if a non-Drupal PHP file is installed in a subdirectory. 
    ExpiresActive Off 
    </FilesMatch> 
</IfModule> 

# Various rewrite rules. 
<IfModule mod_rewrite.c> 
    RewriteEngine on 

    # Block access to "hidden" directories whose names begin with a period. This 
    # includes directories used by version control systems such as Subversion or 
    # Git to store control files. Files whose names begin with a period, as well 
    # as the control files used by CVS, are protected by the FilesMatch directive 
    # above. 
    # 
    # NOTE: This only works when mod_rewrite is loaded. Without mod_rewrite, it is 
    # not possible to block access to entire directories from .htaccess, because 
    # <DirectoryMatch> is not allowed here. 
    # 
    # If you do not have mod_rewrite installed, you should remove these 
    # directories from your webroot or otherwise protect them from being 
    # downloaded. 
    # RewriteRule "(^|/)\." - [F] 

    # If your site can be accessed both with and without the 'www.' prefix, you 
    # can use one of the following settings to redirect users to your preferred 
    # URL, either WITH or WITHOUT the 'www.' prefix. Choose ONLY one option: 
    # 
    # To redirect all users to access the site WITH the 'www.' prefix, 
    # (http://example.com/... will be redirected to http://www.example.com/...) 
    # uncomment the following: 
    # RewriteCond %{HTTP_HOST} !^www\. [NC] 
    # RewriteRule^http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 
    # 
    # To redirect all users to access the site WITHOUT the 'www.' prefix, 
    # (http://www.example.com/... will be redirected to http://example.com/...) 
    # uncomment the following: 
    # RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC] 
    # RewriteRule^http://%1%{REQUEST_URI} [L,R=301] 

    RewriteCond %{HTTP_HOST} !^www\.website\.org$ [NC] 
    RewriteRule .? http://www.website.org%{REQUEST_URI} [R=301,L] 

    # Modify the RewriteBase if you are using Drupal in a subdirectory or in a 
    # VirtualDocumentRoot and the rewrite rules are not working properly. 
    # For example if your site is at http://example.com/drupal uncomment and 
    # modify the following line: 
    # RewriteBase /drupal 
    # 
    # If your site is running in a VirtualDocumentRoot at http://example.com/, 
    # uncomment the following line: 
    # RewriteBase/

    # Pass all requests not referring directly to files in the filesystem to 
    # index.php. Clean URLs are handled in drupal_environment_initialize(). 
    # RewriteCond %{REQUEST_FILENAME} !-f 
    # RewriteCond %{REQUEST_FILENAME} !-d 
    # RewriteCond %{REQUEST_URI} !=/favicon.ico 
    # RewriteRule^index.php [L] 

    # Rules to correctly serve gzip compressed CSS and JS files. 
    # Requires both mod_rewrite and mod_headers to be enabled. 
    <IfModule mod_headers.c> 
    # Serve gzip compressed CSS files if they exist and the client accepts gzip. 
    RewriteCond %{HTTP:Accept-encoding} gzip 
    RewriteCond %{REQUEST_FILENAME}\.gz -s 
    RewriteRule ^(.*)\.css $1\.css\.gz [QSA] 

    # Serve gzip compressed JS files if they exist and the client accepts gzip. 
    RewriteCond %{HTTP:Accept-encoding} gzip 
    RewriteCond %{REQUEST_FILENAME}\.gz -s 
    RewriteRule ^(.*)\.js $1\.js\.gz [QSA] 

    # Serve correct content types, and prevent mod_deflate double gzip. 
    RewriteRule \.css\.gz$ - [T=text/css,E=no-gzip:1] 
    RewriteRule \.js\.gz$ - [T=text/javascript,E=no-gzip:1] 

    <FilesMatch "(\.js\.gz|\.css\.gz)$"> 
     # Serve correct encoding type. 
     Header set Content-Encoding gzip 
     # Force proxies to cache gzipped & non-gzipped css/js files separately. 
     Header append Vary Accept-Encoding 
    </FilesMatch> 
    </IfModule> 
</IfModule> 
+0

한다 RewriteCond % {HTTP_HOST}!^www가 \ .website \ .ORG $ RewriteRule의.? http : //www.website.org% {REQUEST_URI} – dqfan2012

+0

RewriteCond % {HTTP_HOST}^www. (. +) $ [NC] RewriteRule^http : // % 1 % {REQUEST_URI } [L, R = 301] – dqfan2012

답변

2

뭔가가 www.example.com 사이트에 example.com을 리디렉션 작동합니다.

<IfModule mod_rewrite.c> 
    RewriteEngine on 

    RewriteCond %{HTTP_HOST} !^www\. [NC] 
    RewriteRule^http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 

</IfModule> 

여기서는 mod_rewrite 모듈을 활성화했다고 가정합니다.

업데이트 : 또한이 가상 호스트의 http 설정 파일에 다음 선언이 있는지 확인해야합니다.

<Directory /path/to/your/site> 
    AllowOverride All  
</Directory> 
+0

빠른 답변에 감사드립니다. 아파치 HTTP 리다이렉트를 최소한으로 이해했다. 공유 한 리디렉션과 여러 다른 리디렉션을 시도했습니다. 그들 중 누구도 일하지 않습니다. 내가 사용하고있는 환경에 대해 두 가지를 언급하는 것을 잊어 버렸다. 먼저 Drupal 7 웹 사이트에서 작업하고 있습니다. Redirect 모듈은 Drupal 7과 함께 설치됩니다. 이것이 .htaccess 파일에 추가 한 조건 및 규칙을 재정의하는지 여부는 알 수 없습니다. 그럼 다시, 드루팔 (Drupal은 .htaccess 파일에 자신의 규칙이 있습니다. (내가 추가 한 것을 무시할 수도 있습니다). – dqfan2012

+0

위대한! Drupal 7의 .htaccess 파일을 살펴보면,이 작업을하기 위해 라인의 주석을 제거하기 만하면되는 섹션이 있습니다. 이것은 실제로 제가 솔루션을 가져간 곳입니다. 실제 도메인 이름을 입력하지 않고 행을 그대로 두어야합니다. – aaronott

+0

리디렉션이 아직 작동하지 않았습니다. 드루팔 (Drupal의 무언가가 재 작성을 멈추고 있는지 궁금합니다. – dqfan2012