2014-12-29 6 views
0

example.com/profile.php?UserName=xxxx에서 xxxx.example.com xxxx에는 a-z 또는 0-9가 포함되어 있습니다. 나는 (이 사이트에서 일부를) 코드를 시도했지만 내가 원하는대로 작동하지 않았다. 당신은 당신의 리디렉션 규칙 전에 내부 라우팅 규칙을 모두 둘 필요가htaccess는 PHP 파일에서 하위 도메인으로 다시 작성합니다.

RewriteEngine on 
#this should redirect example.com/profile.php?UserName=x to x.site.com 
RewriteCond %{HTTP_HOST} !www.example.com$ [NC] 
RewriteCond %{HTTP_HOST} ^([a-z0-9-_]+).example.com [NC] 
RewriteRule (.*) %1/$1 [QSA,L] 

#if link does not contain subdomain add www 
RewriteCond %{HTTP_HOST} ^example.com 
RewriteRule (.*) http://www.example.com/$1 [R=301,L] 
+1

당신이 원하는 'xxxx.example.com'에'www.example.com/profile.php? UserName = xxxx'의 내용을 보여주기 위해서입니다. 권리? – undone

+0

예 ... xxxx가 변경되며 영문 소문자와 숫자 만 포함될 수 있습니다. – shnisaka

+0

마지막 두 개의 다시 쓰기 규칙을 제거하고 다시 시도하면 어떻게됩니까? – Ben

답변

0

: 여기에 내 현재 코드입니다. R 플래그가있는 규칙이 리디렉션됩니다. PHP 파일 이름을 변경하고 하위 도메인 확장자를 사용하기 위해서는 여기에 좋은 답변 ... 없었다 때문에

RewriteEngine on 

# This redirects the browser to include the "www" 
RewriteCond %{HTTP_HOST} ^example.com [NC] 
RewriteRule (.*) http://www.example.com/$1 [R=301,L] 

# This redirects the browser when profile.php is directly accessed 
RewriteCond %{THE_REQUEST} \ /+profile\.php\?UserName=([a-z0-9-_]+) 
RewriteRule^http://%1.example.com/? [L,R] 

# this internally routes the XXX.example.com to the profile.php script 
RewriteCond %{HTTP_HOST} !www.example.com$ [NC] 
RewriteCond %{HTTP_HOST} ^([a-z0-9-_]+).example.com [NC] 
RewriteRule ^$ /profile.php?UserName=%1 [QSA,L] 
+0

에 액세스 할 때 : example.com/profile.php?UserName=something http://something.example.com/?UserName=something으로 리디렉션됩니다. 어떻게 제거합니까? UserName = something – shnisaka

+0

something.example.com이 페이지를 표시하지 않습니다. example.com/profile.php?UserName=R = 301로 전환하려고 시도한 작업은 여전히 ​​동일한 작업을 수행하고 있습니다. – shnisaka

관련 문제