I have converted URL query to sub-domain, but web-page returns webpage not available.
This is my code in .htaccess, Do i have to do any more settings in apache configuration file.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteCond %{QUERY_STRING} ^user=([^&]+) [NC]
RewriteRule ^(index\.php)$ http://%1.example.com/$1? [L,NC,R=301]
Kindly Help.
Thanks
This can work. Your big problem isn't your .htaccess file, but your lack of DNS records for your subdomains. You need to specify a wildcard at the DNS level. You also need to setup Apache virtual hosts using a wildcard.
As for your .htaccess file, note that it will only match if index.php is present in the URL. To match all requests, like http://example.com/?user=foo, http://example.com/bar.php?user=foo, or http://example.com/baz/bar.php?user=foo, use this instead:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteCond %{QUERY_STRING} ^user=([^&]+) [NC]
RewriteRule ^(.*)$ http://%1.example.com/$1? [L,NC,R=301]
Related
I have the following code in my .htaccess file; it was given to me in an attempt to redirect users from one website to another, but while masking the URL such that the original domain was kept:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^Shmoo.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.Shmoo\.com$ [NC]
RewriteRule ^(.*)$ https://wubbins.com/humnewum/$1 [R=301,L]
RewriteCond %{HTTPS_HOST} ^Shmoo.com$ [NC,OR]
RewriteCond %{HTTPS_HOST} ^www\.Shmoo\.com$ [NC]
RewriteRule ^(.*)$ https://wubbins.com/humnewum/$1 [R=301,L]
I cannot use the generic Web forwarding, redirect, or Alias tools on one.com as these seem to use iFrames and therefore the destination site does not present properly on mobile devices.
This code seems to work, for the most part (as in no iFrame) but the URL is not masked, it is displayed as the destination 'www.wubbins.com/humnewum' not the origin 'www.Shmoo.com'
Complete noob so any help greatly appreciated.
I am in a situation where an user can create his blog in a subdomain.
Users will create blogs and enter the address he wants like say,
abcd.domain.com Then my php code creates a directory called abcd.
To view the blog user will type in abcd.domain.com in his browser and I want a .htaccess code which will rewrite the url and open the files inside the domain.com/abcd
But for the user the url in the browser should stay abcd.domain.com
Currently I am trying this code
RewriteCond %{HTTP_HOST} ^test\.domain\.com$
RewriteCond %{REQUEST_URI} !^test/
RewriteRule ^(.*)$ /test/$1 [L,QSA]
But this gives me 404 even though I have a file test.html inside the test folder and trying to view that page.
Also in this situation I will have to manually make change to the .htaccess file for URL rewrite. What I want to know is if it is possible to have a wild card subdomain redirect to the respective directory.
You can use:
RewriteCond %{HTTP_HOST} ^test\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/test/
RewriteRule ^(.*)$ /test/$1 [L,QSA]
REQUEST_URI with leading /.
With wild card subdomain:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/%1/
RewriteRule ^(.*)$ /%1/$1 [L,QSA]
Note that it takes more than a rewrite rule to have wildcard subdomains. Just fyi.
You need to have created a wildcard DNS record for subdomains and also tell apache to use any subdomain request by having a ServerAlias of *.domain.com in the apache config.
Then try your rule this way and see if it works for you.
RewriteCond %{HTTP_HOST} ^((?!www).+)\.domain\.com$ [NC]
RewriteCond %1::%{REQUEST_URI} !^(.*?)::/\1/?
RewriteRule ^(.*)$ /%1/$1 [L,QSA]
I have a multi-tenant site that spans across 3 different domain names. So for example:
client1.website.com
client2.website.net
client3.website.us
etc
In PHP I can use $_SERVER['HTTP_HOST'] to get the host name and determine if they are on website.com, website.net, etc.
I want to do the same in my .htaccess file as I have a couple of rewrite conditions.
Here they are:
RewriteCond %{HTTP_HOST} ^regionapi\.website\.com
RewriteCond %{HTTP_HOST} !^website\.com$ [NC]
Can I do something like this?
domain = %{HTTP_HOST}
RewriteCond %{HTTP_HOST} ^regionapi\.{domain}
RewriteCond %{HTTP_HOST} !^{domain}$ [NC]
I might be way off but it would be great if this can work.
You should be able to do this by using something such as the following in your .htaccess:
RewriteCond %{HTTP_HOST} (www\.)?regionapi\.example\.(com|net|us)$ [NC]
RewriteRule ^(.*)$ http://example.%2/$1 [R=301,L]
Depending on the domain name extension regionapi.example.(com,net,us) would redirect to:
http://example.(com,net,us)
I have been providing my subdomain name to friend until he manages to get new domain.
Now he has it and I would like to 301 redirect all the old links to his domain:
Example:
http://subdomain.my-domain.com/post.php?118&tg=602643
Redirect to
http://subdomain.com/post.php?118&tg=602643
So I want to keep up all the variables behind the post.php in the redirect
I am .htaccess newbie - can you please help me with providing the correct Rewrite rule?
Also, If you happen to have any good article about .htaccess and how to manage it, link is really appreciated.
Thanks
EDIT
Here are actual usecases I want to do:
redirect
http://raketa2.tasselhof.com/nastenka.php?115&up=648483
to
http://www.raketa2.cz/nastenka.php?115&up=648483
However, since there is no more subdomain existent on my site, i see the 404 error on my main page as this:
/nastenka.php?115&up=648483 -> Provided 404 Error
I tried:
RewriteCond %{Request_URI} ^/nastenka\.php [NC]
RewriteCond %{QUERY_STRING} ^\d+&tg=\d+$ [NC]
RewriteRule ^(.*)$ http://www.raketa2.cz/$1?%{QUERY_STRING} [R=301,L]
But with no good...
SOLVED
Duh! I am really dumb. I just added two new CNAME records to my domain DNS. Should do the trick
Do this:
RewrtiteEngine on
ReWriteBase /
RewriteCond %{HTTP_HOST} ^subdomain\.
RewriteRule ^(.*)$ http://subdomain.com/$1?%{QUERY_STRING} [R=301,L]
The QUERY_STRING post.php?118&tg=602643 will be present.
Assuming that there is no post.php on your domain,
Do this:
RewrtiteEngine on
ReWriteBase /
RewriteCond %{Request_URI} ^/post\.php [NC]
RewriteRule ^(.*)$ http://subdomain.com/$1?%{QUERY_STRING} [R=301,L]
If post.php is present on your domain but, you do not use similar query strings,
Do this:
RewrtiteEngine on
ReWriteBase /
RewriteCond %{Request_URI} ^/post\.php [NC]
RFewriteCond %{QUERY_STRING} ^\d+&tg=\d+$ [NC]
RewriteRule ^(.*)$ http://subdomain.com/$1?%{QUERY_STRING} [R=301,L]
I have Wildcard Subdomains working perfectly on my server, but would like to optimize the .htaccess code to make it more portable. I'm not sure how to do it.
I would like to replace the specific domain name and extension (DOMAIN and .com in the code below) with a regex match so that when using the wildcard code for other domains it is easy to port. So it should be a drop in replacement for domain.com and domain2.net. Here's the code I use:
# Wildcard Subdomain
RewriteCond %{HTTP_HOST} ^(www.)?([^.]+)\.DOMAIN\.com [NC]
RewriteCond %2 !^www$ [NC]
RewriteRule ^(.*)$ http://DOMAIN.com/$1?a=%2 [R=301,QSA,L]
It's a little different than the usual version you see, because I've added the exception for the www. + subdomain condition. It many cases people enter www.sub.domain.com which breaks on the server. So I check for the www. and remove it before redirecting.
Thanks for your help!
The way you currently have it:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www)\.(.*)\.(.*)\.(.*) [NC]
RewriteCond %2 !^www$ [NC]
RewriteRule ^(.*)$ http://%3.%4/$1?a=%2 [R=301,QSA,L]
If you just want to strip the www from the domain:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www)\.(.*)\.(.*)\.(.*) [NC]
RewriteRule ^(.*)$ http://%2.%3.%4/$1 [R=301,QSA,L]