[apache] www가 아닌 ​​www로 일반 htaccess 리디렉션

로 리디렉션 www.example.com하고 싶습니다 example.com. 다음 htaccess 코드는이를 가능하게합니다.

RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

그러나 도메인 이름을 하드 코딩하지 않고 일반적인 방식으로이를 수행 할 수있는 방법이 있습니까?



답변

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

이 작품을 제외하고 는 Michael 과 같습니다 : P


답변

그러나 별도의 http 및 https를 위해이 작업을 수행 해야하는 경우 :

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]


답변

www아닌 사이트www로 리디렉션 (http : https)

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]


답변

httpd.conf 파일에서이 작업을 수행하려면 mod_rewrite없이 수행 할 수 있습니다 (성능이 더 좋습니다).

<VirtualHost *>
  ServerName www.example.com
  Redirect 301 / http://example.com/
</VirtualHost>

나는 그 대답을 여기에있다 : /server/120488/redirect-url-within-apache-virtualhost/120507#120507


답변

www URL을 no-www로 리디렉션하는 규칙은 다음과 같습니다.

#########################
# redirect www to no-www
#########################

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

www가없는 URL을 www로 리디렉션하는 규칙은 다음과 같습니다.

#########################
# redirect no-www to www
#########################

RewriteCond %{HTTP_HOST} ^(?!www\.)(.+) [NC]
RewriteRule ^(.*) http://www.%1/$1 [R=301,NE,L]

NE아파치가 쿼리 문자열을 빠져 나오지 않도록 플래그를 사용했습니다 . 이 플래그가 없으면 Apache는 요청 된 URL http://www.example.com/?foo%20bar을 다음으로 변경합니다.http://www.example.com/?foo%2250bar


답변

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^/(.*)$ https://%1/$1 [R]

RewriteCond에서 캡처 된 모든 HTTP_HOST변수 www. 과에 저장%1 .

RewriteRule캡처 선도적없이 URL /과에 저장합니다 $1.


답변

이 시도:

RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.*)$ %{HTTP_HOST}$1 [C]
RewriteRule ^www\.(.*)$ http://$1 [L,R=301]

호스트가 www로 시작하면 전체 호스트를 URL의 시작 부분에 붙인 다음 “www”를 제거하십시오.