Subdomain rewirte problem - apache

I trying to rewrite subdomain.
For example test.mydomain.com to mydomain.com/test.php
I have create a domain *.mydomain.com.
Now I want it to send data to mydomain.com/test.php when someone visits test.mydomain. com
It will send "test" as a data.
I tried this but its not working
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.+)\.mydomain.com [NC]
RewriteCond %{HTTP_HOST) !^www\.
RewriteRule /test.php?$1 [L]
Any solution?
What's wrong?

Try write this
RewriteRule (.+) /test.php?$1 [L]
Instead of this
RewriteRule /test.php?$1 [L]
Also you can see some basic help

Related

apache Rewrite rule appends /html directory for no reason

I have a domain foo.tech.
I want to use a new domain footech.io instead.
The redirect also has to make sure all the URLs work.
E.g foo.tech/bar goes to footech.io/bar
Here is my .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^foo.tech [NC]
RewriteRule ^(.*) http://footech.io/$1 [R=301,L]
For some reason, it decides to add /html at the end of my domain.
So now if I visit foo.tech it will redirect to footech.io/html
If I visit foo.tech/bar it will redirect to footech.io/html/bar
Please help.
Update:
I think the /html comes from the $1
I've tried to make the rewrite rule as follows:
RewriteRule ^(.*) http://footech.io/$1/$1 [R=301,L]
going to foo.tech leads to footech.io/html//html/
going to foo.tech/bar leads to footech.io/html/bar/html/bar
final update:
I made it work now using this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^foo.tech [NC]
RewriteRule ^html/(.*) http://footech.io/$1 [R=301,L]
This seems to fix it
RewriteEngine On
RewriteCond %{HTTP_HOST} ^foo.tech [NC]
RewriteRule ^html/(.*) http://footech.io/$1 [R=301,L]

htaccess sub domain pointing to a folder but dont want to redirect

I have setup a domain with DNS etc... and wanting to point it to use the /blog folder but when you go to blog.domain.com it just redirects to domains.com/blog - i would like to retain the blog.domain.com url if possible
code im have tried
RewriteCond %{HTTP_HOST} ^blog.domain.com
RewriteRule ^(.*)$ http://domain.com/blog/$1 [L,NC,QSA]
and also tried
RewriteCond %{HTTP_HOST} ^blog\.* [NC]
RewriteRule .* http://domain.com/blog/ [L]
Thanks in advance
This done the trick
RewriteCond %{HTTP_HOST} ^blog.domain.com$
RewriteCond %{REQUEST_URI} !blog/
RewriteRule (.*) /blog/$1 [L]

Apache RewriteRule of multiple variables in query string

I've been trying to figure out how to write a new rewrite rule but I am struggling badly. We are removing the world-wide (WW) site from domain.com and want to forward any old links to the country selector. Any help would be appreciated. Thank you in advance.
# if any user goes to this URL
www.domain.com/products/folder1/folder2/index?sku=123&var2=abc&isocountrycode=ww
# forward them to this URL
www.domain.com/choose/country_selection?ref_url=/products/folder1/folder2/index.cfm?sku=123&var2=abc
# my attempt that does not work
RewriteCond %{QUERY_STRING} ^(.*)isocountrycode=ww(.*)$ [NC]
RewriteRule ^(.+)$ /choose/country_selection.cfm?ref_url=$1?%1%2 [L,R=302]
I have been using this website to test my code http://martinmelin.se/rewrite-rule-tester/
Answered my own question. Helicontech APE requires the second ? to be escaped with a backslash...
RewriteCond %{QUERY_STRING} ^(.+&)?isocountrycode=ww&(.+)?$ [NC]
RewriteRule ^(.+)$ /choose/country_selection.cfm?ref_url=/$1\?%1%2? [NC,R=301,L]
RewriteCond %{QUERY_STRING} ^((.+)&)?isocountrycode=ww$ [NC]
RewriteRule ^(.+)$ /choose/country_selection.cfm?ref_url=/$1\?%2? [NC,R=301,L]

.htaccess change domain but keep path

i don't even know whether this is possible or not.
i've 3 domain names:
mytest.com
test88.com
test99.com
mytest.com is the main domain where all the content is located. in my case it is wordpress which is installed on that webspace.
my htaccess looks like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)test88.com [nc]
RewriteRule ^(.*)$ http://%1mytest.com/wp/?page_id=10&test=test88 [R,L]
RewriteCond %{HTTP_HOST} ^(.*)test99.com [nc]
RewriteRule ^(.*)$ http://%1mytest.com/wp/?page_id=10&test=test99 [R,L]
i want to keep the domainname in case a visitor goes to test88.com but i also want to keep the rest of the path. It should look like this in the address-bar:
http://www.test.88.com/wp/?page_id=10&test=test88
ist this possible?
thanks in advance
Do you want the content from mytest.com show up under the test88.com and test99.com, basically creating duplicates ?
In that case you probably don't want to mod_rewrite to redirect [R], but to reverse-proxy [PT] the content from the main domain.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)test88.com [nc]
RewriteRule ^(.*)$ http://%1mytest.com/wp/?page_id=10&test=test88 [PT,L]
RewriteCond %{HTTP_HOST} ^(.*)test99.com [nc]
RewriteRule ^(.*)$ http://%1mytest.com/wp/?page_id=10&test=test99 [PT,L]
Make sure mod_proxy is installed on your Apache.

Redirect Subdomain to new domain

Hi guys trying to get a 301 redirect working and having trouble. I need to redirect sub.domain1.com to www.domain2.com and make sure that any file names or parameters get sent over with it.
This is what I was trying:
RewriteCond %{HTTP_HOST} ^domain1.com [NC]
RewriteRule ^(.*)$ http://www.domain2.com/$1 [L,R=301]
I also tried this:
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^sub\.domain1\.com$ /www.domain2.com? [R=301,NE,NC,L]
Where am I messing up?
You missed the subdomain part and proper escaping.
RewriteCond %{HTTP_HOST} ^sub\.domain1\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain2.com/$1 [L,R=301]
Further explain can be found in this question.
Rule of thumb for rewriterules: from the most complex to the less complex.
And don't forget the QSA directive (QSA = Query String Append = "make sure that any file names or parameters get sent over with it")
RewriteCond %{HTTP_HOST} ^sub\.domain1\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain2.com/$1 [QSA,R=301,L]
Tell me if it works.