.htaccess point subdomain to file - .htaccess

What I am trying to do is be able to point something like username.domain.com to index.php?url=username. I have been browsing around Google/StackOveflow and have not been able to find anything that will benefit me.
Best Regards.

Add these rules to the htaccess file in your document root:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.)?domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain.com$ [NC]
RewriteCond %{REQUEST_URI} !index.php
RewriteRule ^ /index.php?url=%1 [L]
The first condition makes sure the requested host isn't www.domain.com or domain.com, the next line matches and groups the subdomain name, and the rule itself rewrites everything to /index.php with a query string url and the subdomain (backreferenced using %1).

Related

Point domain to subdomain folder with same url

I have a domain
http://www.example.biz/
I developed a site and put the code in a sub folder on this domain for example, the site is at
http://www.example.biz/site
now, I wanted to point my main domain to this folder so that when user visits example.biz he actually sees example.biz/site and for that, I added the below lines in .htaccess
RedirectMatch ^/$ /site/
it works perfectly however, the url user sees is
http://www.example.biz/site
I do not want that, I wanted user to only see http://www.example.biz as URL but this domain should point to the sub folder invisibly. How should I do that ?
Using mod_rewrite you can use the below rule, now when you access http://www.example.biz it will show the index file from for site/.
RewriteEngine On
RewriteRule ^$ /site/ [L]
Or if you want to show every file try with below,
RewriteEngine On
RewriteRule ^(/|.+)$ site/$1 [L]
I found a simpler solution,
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [NE,R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.biz$
RewriteRule !^site/ /site%{REQUEST_URI} [L]

Wild card URL rewrite of subdomain to subdirectory

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]

.htaccess rewrite for subdomain only

I have the following .htaccess code that successfully rewrites subdomain directories. So if a user visits jim.domain.com they get served placeholder.php?page_type=profile. However, this also affects the main directory http://domain.com. I'd like it so that anything with a subdomain is directed to the placeholder.php page, but using the main directory with no subdomain would act as usual, going to the index.php in the main directory. How would I achieve this? Thanks for any advice.
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^exclude.domain.(.*)
RewriteCond %{HTTP_HOST} !^www.domain.(.*)
RewriteCond %{HTTP_HOST} (.*)\.domain\.com
RewriteRule ^$ placeholder.php?page_type=profile [L,QSA]
If your wanting all requests for test.domain.com to go to domain.com/placeholder.php?page_type=profile as in your comment then you need to include the domain in the rewrite
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^exclude.domain.(.*)
RewriteCond %{HTTP_HOST} !^www.domain.(.*)
RewriteCond %{HTTP_HOST} (.*)\.domain\.com
RewriteRule ^$ http://domain.com/placeholder.php?page_type=profile [L,QSA]
this will not how ever deal with this part
but using the main directory with no subdomain would act as usual, going to the index.php in the main directory
because you have no file/directory miss rules that redirect to index.php. Do you need those, or really just want to solve the first part?
I also wonder if your wanting profile replaced with the subdomain match, in which case you want
RewriteRule ^$ http://domain.com/placeholder.php?page_type=%1[L,QSA]
but both of these examples will not rewrite any path of test.domain.com/longer_url because the ^$ is only matching an empty URL path. But how you want to handling paths needs to be decided before a suitable Reerite can be proposed.

How to forward to new URL including all the variables?

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]

.htaccess Redirections

I've been Googling around for .htaccess redirection information, but nothing I find is quite what I'm looking for.
Basically, I want a solution that will take a site example.com and allow you to enter URL's like:
123.example.com
ksdfkjds.example.com
dsf38jif348.example.com
and this would redirect them to:
example.com/123
example.com/ksdfkjds
example.com/dsf38jif348
So basically accept any subdomain and automatically redirect to a folder on the root of the domain with the name of that subdomain.
Try something like this:
# If we're not on http://example.com
RewriteCond %{HTTP_HOST} .+\.example.com
# Add the host to the front of the URL and chain with the next rule
RewriteRule ^(.*)$ ${HOST}$1 [C,QSA]
# Make the host a directory
RewriteRule ^(.*)\.example\.com(.*)$ http://example.com/$1$2 [QSA]
You don't say what should happen to http://foo.example.com/bar?moo - I've made it go to http://example.com/foo/bar?moo
Change the last line if that's not what you want.
If you just want them to be the entrance:
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule ^ http://example.com/%1 [L,R]
Otherwise:
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule ^ /%1%{REQUEST_URI} [L]

Resources