.htaccess Wildcard Subdomains - .htaccess

I'm trying to implement a solution using .htaccess and wildcard subdomains so that
http://subdomain.example.com is mapped to http://example.com/index.php/accounts/subdomain/. My rules look something like:
RewriteCond %{HTTP_HOST} !www.example.com [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).example.com [NC]
RewriteRule ^(.*/) /index.php [PT,L]
Which works, but disregards everything else. When I try appending anything to the rule e.g:
RewriteRule ^(.*/) /index.php/hello [PT,L]
I get a 500 internal server error. How do I get this working?

You probably need to exclude the index.php from your rule:
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.example\.com$ [NC]
RewriteRule !^index\.php($|/) index.php/accounts/%2%{REQUEST_URI} [PT,L]

This is an adaptation of the code I use to redirect subdomains on my own site. I make no claims to it being best practice but it works;
RewriteCond %{HTTP_HOST} ^(.*)\.com$ [NC]
RewriteCond %1 !^(www)\.example$ [NC]
RewriteRule ^.*$ http://www.example.com/index.php/accounts/%1/ [R=301,L]

Try changing your RewriteRule to
RewriteRule ^/(.*)$ /index.php/accounts/%1/$1 [PT]
That will rewrite the URL to one that includes the subdomain and the original request URI.
EDIT: maybe it needs to be
RewriteRule ^(.*)$ /index.php/accounts/%1/$1 [PT]
as mentioned in the comments.

Related

Domain specific rewrite rule in htaccess?

I currently have this in my htaccess:
RewriteRule /order/?$ /lang-en/order.php [QSA,L]
Since I have several domains pointing to this ftp account, I'd like to have domain specific rewrite rules.
The following doesn't work, but you get the idea:
RewriteRule https://www.shakeplus.com/order/?$ /lang-en/order.php [QSA,L]
RewriteRule https://www.shakeplus.co.uk/order/?$ /lang-gb/order.php [QSA,L]
I may need a RewriteCond %{HTTP_HOST}, but have no clue how to do it. Any ideas?
This question does ok in search results for some queries, so worth an answer.
Match both with and without www:
RewriteCond %{HTTP_HOST} ^(www\.)?shakeplus\.com$ [NC]
RewriteRule order/?$ /lang-en/order.php [QSA,L]
RewriteCond %{HTTP_HOST} ^(www\.)?shakeplus\.co\.uk$ [NC]
RewriteRule order/?$ /lang-gb/order.php [QSA,L]
or just match with www:
RewriteCond %{HTTP_HOST} ^www\.shakeplus\.com$ [NC]
RewriteRule order/?$ /lang-en/order.php [QSA,L]
RewriteCond %{HTTP_HOST} ^www\.shakeplus\.co\.uk$ [NC]
RewriteRule order/?$ /lang-gb/order.php [QSA,L]
You may want to do a 301 redirect which requires [QSA,R=301,L] in place of [QSA,L]

.htaccess RewriteRule subsubdomain.subdomain.domain.tld to subdomain.domain.tld/subsubdomain

Is it posible to redirect a sub-subdomain into a subdomain with the sub-sub domain as new folder using .htaccess rewrite-rule?
For example... When i go to 2013.archive.example.com I want to end up in archive.example.com/2013..
Already tried some things, current .htaccess is:
RewriteRule On
RewriteCond %{HTTP_HOST} ^(.*)\.archive\.example\.com$
RewriteRule ^(.*)$ archive.example.com/%1/$1 [L]
Unfortunately it isn't working. Any suggestions for this?
Changed it a but, currently using:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.archive\.example\.com$ [NC]
RewriteRule ^(.*)$ %1/$1 [L]
and is working exactly how I wanted:)
For external redirect: (URL change in browser)
You can use this rule in your DOCUMENT_ROOT/.htaccess:
RewriteCond %{HTTP_HOST} ^([^.]+)\.(archive\.example\.com)$ [NC]
RewriteRule ^ http://%2/%1%{REQUEST_URI} [L,R=301]
This will work provided DOCUMENT_ROOT is same for anything.archive.example.com and archive.example.com.
For internal forward: (No URL change in browser)
You can use this rule in your DOCUMENT_ROOT/.htaccess:
RewriteCond %{HTTP_HOST} ^([^.]+)\.archive\.example\.com$ [NC]
RewriteRule ^ /%1%{REQUEST_URI} [L]
If they share the same root, then you don't need the archive.example.com part in your rule's target:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.archive\.example\.com$ [NC]
RewriteRule ^(.*)$ /%1/$1 [L]

Redirect all non www to www except one subdomain with htaccess

I'd like to redirect all URLs not containing www to www
I used this:
RewriteCond %{HTTP_HOST} !^www.example.com$
RewriteRule ^(.*) http://www.example.com$1 [QSA,L,R=301]
Now I need one subdomain not to be redirect like this, or I get an error.
So I'd like to redirect any URL that does not begin with sub or with www to www.example.com
How can I do this ? thanks
You need a second RewriteCond. You can apply as many as you like to a RewriteRule.
Assuming anything that is not sub.mydomain.com needs to be www.mydomain.com, here is your code:
RewriteCond %{HTTP_HOST} !^sub.mydomain.com$
RewriteCond %{HTTP_HOST} !^www.mydomain.com$
RewriteRule ^(.*) http://www.mydomain.com/$1 [QSA,L,R=301]
But you can simplify this further using the pipe (|) character in Regex:
RewriteCond %{HTTP_HOST} !^(sub|www).mydomain.com$
RewriteRule ^(.*) http://www.mydomain.com/$1 [QSA,L,R=301]
RewriteCond %{HTTP_HOST} !^(.*)\.(.*)\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
This works for all domains, excluding any subdomains. have fun.
I tried the answer by Scott S but it didn't work, so I modified to use the one I was using for general 301:
RewriteCond %{HTTP_HOST} ^domain.com
RewriteCond %{HTTP_HOST} !^subdomain.domain.com
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]
And it works like a charm

RewriteRule at htaccess wrong redirect

ive this rules at htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^site.com [NC]
RewriteRule ^(.*)$ http://www.site.com/$1 [L,R=301]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^warcraft$ www.site.com/forumdisplay.php?f=480 [R=301,NE,NC,L]
issue happend when redirect warcraft its redirect to
http://www.site.com/home/site/public_html/www.site.com/forumdisplay.php?f=480
any tip ?
I suppose you want to redirect it to http://www.site.com/forumdisplay.php?f=480
If so put it in your .htaccess file:
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^warcraft$ http://www.site.com/forumdisplay.php?f=480 [R=301,NE,NC,L]
You must provide the full URL including protocol in your rule, in this case http://www.site.com/.....

How do I Rewrite URL from some-sub-domain.some-domain.com to some-domain.com/some-sub-domain

I want to rewrite my URL, so that I can reach the folder which is seen by the clients as sub-domain. Client will put the following in the URL Bar:
abc.def.com
and by URL rewrite it will be gone to
def.com/abc
silently. Please tell me how can I do that ?
You should use %{HTTP_HOST} instead of %{REMOTE_HOST}. This is because %{REMOTE_HOST} is the result of reverse DNS lookup on %{REMOTE_ADDR} if HostnameLookups is enabled.
So your rule should be
RewriteCond %{HTTP_HOST} ^(?:www\.)?([^\.]+)\.your_domain\.com [NC]
RewriteRule ^(.*) /%1/$1 [L]
There is a very few change in your RewriteRule to work this well
RewriteCond %{HTTP_HOST} ^(?:www\.)?([^\.]+)\.your_domain\.com [NC]
RewriteRule ^(.*)$ /%1/$1 [L]
You should use $ in the RewriteRule. Hope this helpful for you.
If you already know the subdomain, you can:
RewriteCond %{REMOTE_HOST} ^(?:www\.)?subdomain\.your_domain\.com [NC]
RewriteRule ^(.*) /subdomain/$1 [L]
And if you wish to support ANY possible subdomain, use:
RewriteCond %{REMOTE_HOST} ^(?:www\.)?([^\.]+)\.your_domain\.com [NC]
RewriteRule ^(.*) /%1/$1 [L]

Resources