Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
611 views
in Technique[技术] by (71.8m points)

.htaccess - htaccess, redirect virtual subdomain to URL parameter

I have a .ne.ro domain (registered in Romania) where visitors can enter with or without 'www':

http://mydomain.ne.ro
http://www.mydomain.ne.ro

I want to redirect http://123.mydomain.ne.ro to http://mydomain.ne.ro?id=123. If visitor enter with 'www|mail|ftp' must be treated as non parameter (obviously).

I tried htaccess subdomain redirct with last url parameter but didn't work.

Current code:

# edited on 05/April/2011 as suggested:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.mydomain.ne.ro$
RewriteCond %{HTTP_HOST} ^(.*).mydomain.ne.ro$
RewriteRule ^$ /index.php?id=%1 [R,L]


Hosted on a hostgator's business plan (linux) and registered at nic.ro, just if asking. See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

After many suggested code (thanks supersuphot) and a lot of research finally I found an answer.

First I added the subdomain * under mydomain.ne.ro, pointed to domain folder/path:

Create subdomain *, hostgator shared account

Then I uploaded .htaccess file with the next code:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.mydomain.ne.ro
RewriteCond %{HTTP_HOST} ^(.+).mydomain.ne.ro
RewriteRule ^([^/]*)$ http://mydomain.ne.ro/?id=%1 [P,L]

this works too:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.mydomain.ne.ro$
RewriteCond %{HTTP_HOST} ^(.*).mydomain.ne.ro$
RewriteRule ^$ /index.php?id=%1 [P,L]

The most important thing is the P instead R at last line, to keep original subdomain. Check this answer to a related question.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...