Rewrite folder to subdomain - .htaccess

I want to rewrite all addresses after this
http://www.mydomain.com/questions/*
to this
http://*.mydomain.com/
for example:
http://www.mydomain.com/questions/example
http://example.mydomain.com/
Can anyone help me in this issue!?
Thanks

There you go.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.mydomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^.mydomain\.com$
RewriteRule ^questions/([^/]+)/?$ http://$1.mydomain.com/ [NC,R=301,L]
Plug this into your .htaccess file and you should be good to go. :)

Related

How to redirect dynamic subdomains with multiple query parameters

I would like to redirect
domain.com/test.php?username=username&code1=849&code2=4d1
to
username.domain.com/849/4d1
with .htaccess
At the moment I have
RewriteRule ^test/([^/]+)/([^/]+)/([^/]+) test.php?username=$1&code1=$2&code2=$3 [NC]
RewriteCond %{HTTP_HOST} ^(^.*)\.domain.com
RewriteRule ^(.*) test.php?username=%1&code1=%2&code2=%3 [NC]
The redirect works but the code (php)
$code1 = $_GET['code1'];
echo $code1;
is empty. Can anyone point me in the right direction?
Thanks
Found it!
So if you want to redirect
username.domain.com/849/4d1
to
domain.com/test.php?username=username&code1=849&code2=4d1
You need to add below code to your .htaccess file:
RewriteCond %{HTTP_HOST} ^(^.*)\.domain.com
RewriteRule ^(.*)/([^/]+) test.php?username=%1&code1=$1&code2=$2 [NC]

htaccess redirect subdomain into folder that has similiar name with subdomain

I did many way to do this, but always getting 404 error.
I want to redirect subdomain
[username].domain.com/
to
domain.com/user/[username]/index.php
and the url on browser address is not changed still
[username].domain.com/
[username] is dynamic according to registered user in my site.
my last trial httaccess setting is
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(^.*)\.domain\.com$
RewriteRule ^ ^.domain.com/user%{REQUEST_URI} [L,P]
Note: I did set my DNS wildcard *.domain.com
Thanks in advance,
Nanang K
============================================================
SOLVED
I had to create 3 htaccess files.
in root directory (redirect to user)
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(^.*)\.domain.com
RewriteRule ^(.*)$ /user/$1 [NC,L]
in user directory (redirect to username_folder)
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^([aA-zZ])$ user/$1/index.php
RewriteCond %{HTTP_HOST} ^(^.*)\.domain.com
RewriteRule (.*) user/%1/index.php
in username_folder directory (rewrite the url into original url)
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^([aA-zZ])$ index.php?siteName=$1
RewriteCond %{HTTP_HOST} ^(^.*)\.domain.com
RewriteRule (.*) index.php?siteName=%1
now, when user access robert.domain.com, he will get the content from domain.com/user/robert/index.php. And the url browser still robert.domain.com
hope helps someone who get similiar problem.
Hello Can you please try this :-
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteRule ^(.*)$ /users/$1 [L,NC]
It may use helpful.
It should be like this, try it out.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^[username].domain.com$
RewriteRule ^/?$ "http\:\/\/domain\.com\/users\/" [R=301,L]
hope this helps!

.htaccess rewrite insert new url segment e.g. www.domain.com/wildcard == www.domain.com/new_segment/wildcard/

I would like to do this globally via htaccess rewrite so any www.domain.com/wildcard/ would == www.domain.com/new_segment/wildcard/ but I can't seem to figure this out.
So far, I have the following:
Options +FollowSymLinks -MultiViews
RewriteCond %{HTTP_HOST} ^domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteRule ^(.*)$ "http\:\/\/www\.domain\.com\/new_segment\/$1" [R=301,L]
For some reason, on the initial page load I get www.domain.com/new_segment/new_segment/ but if I go to www.domain.com/old_segment I would expect www.domain.com/new_segment/old_segment/ but I get www.domain.com/old_segment/ instead.
Any help would be greatly appreciated.
Why would you want that? Anyways, you can do it easier by putting desired files into a directory and a redirection from a file on the /old_segment to /new_segment.
This can be done with:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/new_segment
RewriteRule ^(.*)$ /new_segment/$1 [R=301,L]
It will check to see if the new_segment isn't in the url, if it is then nothing will be redirected. If it isn't then new_segment will be added.

.htaccess simplification / better solution?

I'm trying to tweak some rewrites in an .htaccess file but my regexp/mod_rewrite knowledge is thin!
We have primarydomain.com and secondarydomain.co.uk, and we want to rewrite anything and everything to www.primarydomain.com
At present I'm doing this:
RewriteCond %{HTTP_HOST} ^secondarydomain.co.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www.secondarydomain.co.uk$ [OR]
RewriteCond %{HTTP_HOST} ^primarydomain.com$
RewriteRule (.*)$ http://www.primarydomain.com/$1 [R=301,L]
.. but I'm sure there is a better way?? Is there a rule that would rewrite any host that's not www.primarydomain.com? I tried playing about with the not operator (!) and managed to cause a redirect loop so don't want to do that :/
Any help appreciated! Ben
Ok, Found it myself:
RewriteEngine On
RewriteRule ^(.*)$ http://www.primarydomain.com/ [R=301,L]

how to redirect www url to non-www url with htaccess?

I wanted to know how to redirect users from http://www.mysite.com to http://mysite.com using htaccess, I'm not familiar in how to manipulate this file in order to do this, can anyone help me accomplish this?
Thanks in advance.
This site gives a complete answer:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^domain\.com
RewriteRule (.*) http://domain.com/$1 [R=301, L]
It was the first thing that appeared when I googled for it, so I assume you didn't even bother to look for an answer (naughty boy, tsk, tsk!).
This site should be helpful:
http://enarion.net/web/htaccess/redirect-www-and-no-www/
Just add this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule (.*) http://mysite.com/$1 [R=301,L]
something like this should do the trick
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^www\.(.*)$ $1 [NC]
you want to google for "htaccess rewrite" to find more info about it

Resources