.htaccess : Rewriting initial output back into sub

2019-08-02 09:20发布

问题:

I have the following htaccess.

Options +FollowSymlinks 
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^(www|m)\.DOMAIN\.com$
RewriteCond %{HTTP_HOST} ^([^\.]+).DOMAIN.com$
RewriteRule ^(.*)$ http://DOMAIN.com/index.php?id=%1 [NC,QSA,L,R=301]

The problem is that although it works I need it to still use the subdomain as the URL. So basically now I'm trying to rewrite DOMAIN.com/index.php?id=%1 back into SUDOMAIN.DOMAIN.com so the user sees the subdomain they requested on the address bar and not the http://domain.com/index.php?id=user

I'ver tried all sorts of things but always end up with some sort of infinite loop. Please help!


I already have a wildcard subdomain set up. In conjunction with the htaccess shown above. The submain doesn't stick, it rewrites itself into the long string shown above. But what im trying to do is get the url ro remain subdomain.domain.com unfortunately the link you posted does peretty much the same as what I have posted above, doesn't really seem to be able to get the sub-domain to stick.

Any ideas?

回答1:

Not sure if you figured it out yet, but your last line (I believe) should be this:

RewriteRule ^(.*)$ index.php?id=%1 [NC,QSA,L,R=301]

If that does not work try this:

RewriteRule (.*) index.php?id=%1 [NC,QSA,L,R=301]

No expert but I have subdomains working from some other code I found. Hope it helps.



回答2:

You need to change your DNS settings before. (A record)

There is already an answer for that question.