.htaccess sub domain as file parameter with all the variables - .htaccess

I want to make this url redirect using .htaccess (linux/apache) ..
I want to keep "as is" all calls for www sub domain like this
www.example.com/index.php
www.example.com/test.php?id=1&name=a
www.example.com/whatever.php?whateverurl=xxxx
but I want to redirect all wild card urls like this with the sub domain as extra variable to current unlimited-whatever url variables
for example
xxx.example.com -> example.com/?subdomain=xxx
yyy.example.com/index.php -> example.com/index.php?subdomain=yyy
whatever.example.com/test.php?var=1 -> example.com/test.php?subdomain=whatever&var=1
whatever.example.com/whatever.php?var1=1&var2=2 -> example.com/whatever.php?subdomain=whatever&var1=1&var2=2
is this possible?
Thank you

Once you set up your Wildcard DNS Record you should make your .htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.example\.com$
RewriteRule ^(.*)\??(.*)?$ $1?host=%{HTTP_HOST}&%{QUERY_STRING} [L] [L]
This rule works, although it will provide you the whole host, rather than just the subdomain, which you'll have to parse in your PHP.

Thank you for your answer
this rule seems to works
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$ [NC]
RewriteRule ^/?(.*)$ $1?subdomain=%1 [QSA,L,NE]
for the time without problems ...
thanks again for your answer
Best regards
Vangelis

Related

htacces rewrite url - redirect url to sub folder without www - oc

I want to redirect url to a sub folder without www. like,
If anyone type
http://WWW.mywebsite.com/theme1/
Then, I want this result,
http://mywebsite.com/theme1/ (without www)
theme1 is a folder.
Thanks in advance.
Try it like this,
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R,QSA,L]
Dear This can be done via masking, you can use https://bitly.com/ to shorten your website url or you can use service provider such as GoDady, to get the same

.htaccess rewrite domainnames, save subdomain and parameters

i have 2 domains but want use only one.
these some examples of domains...
x://test1.domain1.xx/parameter/1
x://test1.domain1.xx/parameter/2
x://test1.domain1.xx/parameter/3
x://test2.domain1.xx/parameter/1
x://test2.domain1.xx/parameter/2
x://test2.domain1.xx/parameter/3
x://test1.domain2.xx/parameter/1
x://test1.domain2.xx/parameter/2
x://test1.domain2.xx/parameter/3
x://test2.domain2.xx/parameter/1
x://test2.domain2.xx/parameter/2
x://test2.domain2.xx/parameter/3
now i want only use xx.domain2.xx/params... all sub/domains with maindomain "domain1" should be rewritten or redirect. without loose parameters or subdomain.
thanks a lot.
I tried sometimes an i think i got a solution.
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).domain1.xx [NC]
RewriteRule (.*) http://%2.domain2.xx/$1 [R=301,L]

htaccess rewrite rule for subdomain results to redirect loop

Below is my htaccess config
RewriteEngine On
# if the domain starts with "apply."
# i.e. apply.domain.com
# redirect to application URI
RewriteCond %{HTTP_HOST} ^apply\. [NC]
RewriteRule !^formwizard /formwizard/apply [R=301,L,NC]
I have tried it, and when I go to http://apply.domain.com/ it successfully redirects to http://apply.domain.com/formwizard/apply.
My problem is, once it redirects to that URI, it goes to a redirect loop.
Can anyone please help hit me what is wrong with my config?
Background:
I have 3 subdomains: www.domain.com, apply.domain.com, and admin.domain.com. It all references to the same source codes and just managed by my htaccess.
Once the person goes to apply.domain.com, it should be redirected to the application page which is /formwizard/apply.
PS. I don't want to depend too much on the backend codes because I believe this is a server config problem.
Thanks in advance!
If there are more rules then this rule might be getting impacted by them. Modify this rule to this:
RewriteCond %{HTTP_HOST} ^apply\. [NC]
RewriteCond %{THE_REQUEST} !/formwizard/ [NC]
RewriteRule ^ /formwizard/apply [R=301,L,NC]
Also make sure this is very first rule below RewriteEngine On line.

htaccess redirect specific url to another domain

I want to redirect specific URL with params to another domain
http://domain.tld/buy/?w=X1234 to http://anotherdomain.tld/product.php?id=X1234
Here my htaccess
RewriteCond %{REQUEST_URI} !^/*buy/(.*)$ [NC]
RewriteRule ^/*buy/?w=([a-zA-Z0-9]+)$ http://anotherdomain.tld/product.php?id=$1 [NC,L,R=301]
but not working. maybe the problem is param ?w=
Thank you
UPDATE: i solve the problem by using Query string, after reading How to REGEX and .htaccess rewrite url
RewriteCond %{QUERY_STRING} ^w=([a-zA-Z0-9]+)$
RewriteRule ^/*buy/$ http://anotherdomain.tld/product.php?id=%1 [NC,L,R=301]
Thank you stackoverflow and folks! i m start understanding regex from here ^_^
That's right, your params won't be picked up, but they can be passed on.
Something like;
RewriteRule ^buy/([a-zA-Z0-9]+)$ http://anotherdomain.tld/product.php?id=$1 [NC,L,R=301]
Where your original url would be; /buy/X1234

How to create virtual username.domain.com using .htaccess mod rewrite

I know there are plenty of questions like I asked, but searching all day the soluation, I didnt find right one that would work for me, therefore I asked question.
Here is .htaccess code I have to view user profiles:
RewriteRule ^users/([^/\.]+)/$ viewProfile.php?user=$1&%{QUERY_STRING}
RewriteRule ^users/([^/\.]+)/([^/\.]+)/$ viewProfile.php?user=$1&usr_profile=$2&%{QUERY_STRING}
And using this rewrite users profile URL is:
http://www.domain.com/users/username.html
Inside of this type of URL, I would like to create subdomain users URL, like:
http://username.domain.com
Can anyone suggest the solution?
Thanks a lot.
Make sure your vhost is setup to accept requests for *.domain.com. Then add this above your other rewrite rules:
RewriteCond %{HTTP_HOST} !^www [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.com$ [NC]
RewriteRule ^$ viewProfile.php?user=%1 [L,QSA]

Resources