开发者

Mod rewrite for fake subdomains?

开发者 https://www.devze.com 2022-12-23 11:19 出处:网络
I really tried tons of methods but i\'m not successful. I want a .Htaccess code to do the following : I want to rename this : http://www.mydomain.com/media --> http://media.my开发者_高级运维domain.co

I really tried tons of methods but i'm not successful. I want a .Htaccess code to do the following :

I want to rename this : http://www.mydomain.com/media --> http://media.my开发者_高级运维domain.com

So, By example instead of calling this : http://www.mydomain.com/media/XXX/picture.jpg i will call : http://media.mydomain.com/XXX/picture.jpg

Thank you


It's gonna be like this:

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


Make sure rewrite_module is loaded, something like;

LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so

Then add the following (to your .htaccess):

RewriteEngine on
RewriteCond %{HTTP_HOST} www.mydomain.com
RewriteRule ^/([^/]*)(.*) http://$1.mydomain.com$2 [L,R]

The Cond will only match www.mydomain.com. The Rule then will split the URL into 2 using the first '/' (which will be included in $2), rewrite and redirect.


If you want the opposite (see Roger's comment) and without redirecting the user

RewriteEngine on
RewriteCond   %{HTTP_HOST}                 !^www\.mydomain\.com$
RewriteRule   ^(.+)                        %{HTTP_HOST}$1          [C]
RewriteRule   ^([^.]+)\.mydomain\.com(.*)  http://www.mydomain.com/$1$2 [L]

Also, see here: http://httpd.apache.org/docs/1.3/misc/rewriteguide.html

0

精彩评论

暂无评论...
验证码 换一张
取 消