.htaccess Dynamic sub domains - php

I want to have dynamic subdomains, such as:
(.*).mydomain.com
that I can retreive with $_GET['subdomain']. I have added an wildcard DNS record (* A xx.xx.xx.xx), and now for every subdomain I go to (e.g. test.mydomain.com) all I get is:
Apache is functioning normally
I have tried this code in .htaccess, but nothing seems to help:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.mydomain\.com$
RewriteRule ^(.*)$ http://www.mydomain.com/?subdomain=%1 [L]
Thanks in advance!

Are you looking for something more like this?
RewriteCond %{QUERY_STRING} !subdomain=
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.mydomain\.com$ [NC]
RewriteRule ^(.*)$ /$1?subdomain=%1 [L,QSA]

Related

.htaccess redirect a folder in subdomain to a folder in main domain

Here's what I need.
I need to redirect (301) all content of blog.example.com/sample/ to example.com/sample/ and I'd like to accomplish this using htaccess.
Here's what I tried, but it didn't work:
RewriteCond %{HTTP_HOST} ^blog\.example\.com/sample [NC]
RewriteRule ^(.*)$ https://www\.example\.com/sample/$1 [L,R=301]
Thanks for the help.
RewriteCond %{HTTP_HOST} ^blog\.example\.com/sample [NC]
Here having /sample is problematic because HTTP_HOST only matches host name part in a request so it will never match this condition.
You can use this rule instead:
RewriteCond %{HTTP_HOST} ^blog\.(example\.com)$ [NC]
RewriteRule ^sample/ https://www.%1%{REQUEST_URI} [L,R=301,NC,NE]

.htaccess rewrite all, except one page

I am trying to solve a problem with my .htaccess file.
I would like to use 301 redirect from my "old" doman to the "new" one, except some certain pages. Like "PageA" and "PageB".
The code thats working and iam using for redirect all is this:
RewriteCond %{HTTP_HOST} ^old.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.old.com [NC]
RewriteRule ^(.*)$ http://www.new.com/$1 [L,R=301,NC]
"PageA", and "PageB" has to be redirected to the "new" domain too, but to a certain address. Like:
old.com/pageA ---> new.com/something/pageA
old.com/pageB ---> new.com/something2/pageB
What code should i add to .htaccess, to add some exceptions with certain addresses ?
Try below rule,
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/(pageA|pageB)$
RewriteRule ^ http://www.new.com/test/%1 [L,R=301,NC]
RewriteCond %{HTTP_HOST} ^old.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.old\.com [NC]
RewriteRule ^ http://www.new.com/$1 [L,R=301,NC]

Apache .htaccess rewrite multiple condition with parameters by subdomain

I currently have this in my .htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(.*).domain\.com
RewriteRule ^(.*)$ index.php?id=%1 [L,NC,QSA]
All my subdomain regardless is abc.domain.com or def.domain.com will be pointing towards index.php?id=abc and index.php?id=def respectively.
This is currently working perfectly fine on my website however I have other file path like domain.com/faq.php, Hence when user access abc.domain.com/faq.php , this will be pointing to faq.php?id=abc as well.
So my question is how to capture the file path parameter and rewrite with the parameter being pass?
Thank you for the help and please let me know if you require any other information. I'm currently new to rewrite mod hence, this might be a duplicate question.
You can try this rules:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(.*).domain\.com
RewriteRule ^(.*\.php)$ $1?id=%1 [L,NC,QSA]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(.*).domain\.com
RewriteRule ^(.*)$ index.php?id=%1 [L,NC,QSA]

Redirect sub domain to sub directory

I want to redirect the the subdomain to subdirectory but not working. Here is my efforts.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^name\.site_url
RewriteRule ^(.*)$ http://site_url/name/$1 [L,R=301]
Try this:
RewriteCond %{HTTP_HOST} ^sub\.
RewriteRule ^(.*)$ http://your_domain/sub/$1 [R,L]
Instead sub in RewriteCond and RewriteRule you can place whatever you want
Btw, RewriteCond %{HTTP_HOST} ^sub\.my\.domain$ also works for me. So, check your site url. Or give more info (at least, what happened in apache error.log).

redirect folder subdomain to subdomain.domain.com in htaccess

I have a domain name domain.com and some folders inside it. When i am accessing the folders inside this domain i can access the folder like this.
http://www.domain.com/folder
I want to convert this url with htaccess to
http://www.folder.domain.com
I would like to have similar urls for all folders so its a wildcard entry.
Please tell me how can it be done with htaccess.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com
RewriteRule ^(.*)$ http://subdomains.domain.com/$1 [L,NC,QSA]
You can use:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?:www\.)?domain\.com [NC]
RewriteRule ^([^/]+)(/.*)?$ http://www.$1.domain.com$2 [R=301,L,NC]
RewriteCond %{HTTP_HOST} ^www\.(.+)\.domain\.com [NC]
RewriteRule ^(.*)$ /%1/$1 [L]
To redirect requests to www.example.com/blog to blog.example.com, try this :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule ^blog/(.*)$ http://blog.example.com/$1 [L,QSA,R=301]
RewriteCond %{HTTP_HOST} ^blog\.example\.com$
RewriteCond %{REQUEST_URI} !^blog/
RewriteRule ^(.*)$ /blog/$1 [L,QSA]
For other you have to set dynamic name and pass as parameter, for simple blog you can take help of this question.

Categories