.htaccess - Redirect subdomain to folder
I have set CNAME of my sub domain below:
blog.mydomain.com
to my wordpress that installed in folder /blog/
under root directory.
Formerly I need to use this url to call wordpress:
http://blog.mydomain.com/blog/
which is ugly. I have tried many code to redirect:
http://blog.mydomain.com/
to the folder so I can use it as my wordpress url.
Finally I got .htaccess
setting that is work:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^blog\.mydomain\.com$
RewriteCond %{REQUEST_URI} !^/blog/
RewriteRule (.*) /blog/$1
I have also CNAME other subdomain: http://forum.mydomain.com
to mybb installation in folder /forum/mybb/
so the .htaccess
need to put [L]
on each of RewriteRule code as below.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^forum\.tophyips\.info$
RewriteCond %{REQUEST_URI} !^/forum/mybb/
RewriteRule (.*) /forum/mybb/$1 [L]
RewriteCond %{HTTP_HOST} ^blog\.tophyips\.info$
RewriteCond %{REQUEST_URI} !^/blog/
RewriteRule (.*) /blog/$1 [L]
In case you want to use the code please don't forget to set the site url and cookie path in the application config file follow to the setting to make the redirection work properly.
.htaccess redirect subdomain to directory
To map example.com or www.example.com to /front and sub.example.com to /subdomain folder, you can use these rules :
RewriteEngine on
#rewrite main domain to /front folder
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule !front /front%{REQUEST_URI} [NC,L]
#subdomain to /subdomain folder
RewriteCond %{HTTP_HOST} ^sub\.example\.com$
RewriteRule !subdomainfolder /subdomainfolder%{REQUEST_URI} [NC,L]
Redirecting subdomain to subdirectory through htaccess
Try this in your .htaccess:
RedirectMatch 301 wiki.comp.tf(.*) comp.tf/wiki$1
If that does not work, an alternative option is:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^wiki.comp.tf
RewriteRule ^(.*)$ http://comp.tf/wiki$1 [L,NC,QSA]
Some potential options for you using actual names:
Redirect 301 wiki.comp.tf comp.tf/wiki
Subdomain redirect to the sub directory with folder name .htaccess
for First condition use,
RewriteRule ^()$ /subdomains/index.php?user=$1
and for Second condition use,
RewriteRule ^mycategory$ /subdomains/category.php?user=$1
Redirect subdomain and folder by htaccess
HTTP_HOST
only matches host/domain name. It doesn't match URI.
You may use:
RewriteCond %{HTTP_HOST} ^it\.(example\.com)$ [NC]
RewriteRule ^it(?:/|$) https://%1%{REQUEST_URI} [R=301,L,NE.NC]
htaccess redirect all subdomains to the same directory
Your above .htaccess would externally redirect the calls, as you use a full URL as the target.
In your question you say you want to keep the hostname, so I will assume that is the requirement.
0) Enable rewrite engine
RewriteEngine On
1) Rewriting known subdomains to their directory in /subdomains
# Rewrite known subdomains to /subdomains/{subdomain}/
RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$
RewriteCond %{REQUEST_URI} !^/subdomains [NC]
RewriteCond %{REQUEST_URI} !^/404 [NC]
RewriteRule ^(.+)$ /subdomains/%1/ [L]
- When we encounter a request with a subdomain,
- and we have not rewritten it to /subdomains
- and we have not rewritten it to /404
- then rewrite it to /subdomains/{subdomain}/
So, if the request was
http://foo.example.com/hello
the URL in the browser would stay the same, but internally be mapped to
/subdomains/foo/
2) Rewriting unknown subdomains to /404
# Rewrite missing unknown subdomains to /404/
RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$
RewriteCond %{REQUEST_URI} ^/subdomains [NC]
RewriteCond %{REQUEST_URI} !^/404 [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ /404/ [L]
- When we encounter a request with a subdomain,
- and we have already rewritten it to /subdomains
- and we have not rewritten it to /404
- and it is not existing as a file
- and it is not existing as a directory
- and it is not existing as a symlink
- then rewrite it to /404
So, if the request was
http://bar.example.com/hello
the URL in the browser would stay the same, but internally be mapped to
/subdomains/bar/
by the first RewriteRule
from 1).
If /subdomains/bar/
does not exist, the second RewriteRule
from 2) will kick in and internally map it to
/404/
3) Test-Environment
I actually tested all of this with exemplary code, available here: https://github.com/janpapenbrock/stackoverflow-36497197
Related Topics
How to Install Pdo_Sqlsrv on My Windows 2008 Server 2008 R2
MySQL Select Records Greater Than 3 Months
PHP Warning: MySQLi_Close() Expects Parameter 1 to Be MySQLi
Converting an Array from One to Multi-Dimensional Based on Parent Id Values
Are PHP Associative Arrays Ordered
Cast the Current Object ($This) to a Descendent Class
How to Count Same Values in an Array and Store It to a Variable
Fatal Error: Call to Undefined Method MySQLi_Result::Fetch_All()
PHP Foreach by Reference Causes Weird Glitch When Going Through Array of Objects
How to Pass Objects by Reference in PHP 5
Sending Xml Data Using Http Post with PHP
Refresh a Table with Jquery/Ajax Every 5 Seconds