Is it possible to use .htaccess
to rewrite a sub domain to a directory?
Example:
http://sub.domain.example/
shows the content of
http://domain.example/subdomains/sub/
Is it possible to use .htaccess
to rewrite a sub domain to a directory?
Example:
http://sub.domain.example/
shows the content of
http://domain.example/subdomains/sub/
Try putting this in your .htaccess
file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub.domain.example
RewriteRule ^(.*)$ /subdomains/sub/$1 [L,NC,QSA]
For a more general rule (that works with any subdomain, not just sub
) replace the last two lines with this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.example
RewriteRule ^(.*)$ subdomains/%1/$1 [L,NC,QSA]
http://domain.com/subdomains/test
? And you want the redirection to be invisible? –
Cilo P
flag in addition to the L
. –
Cilo http://test.domain.com/subdomains/test/
, when I only want it to show http://test.domain.com/
–
Combine test.domain.com/subdomains/test
- maybe there is something else going on here. Can you add RewriteBase /
right after RewriteEngine on
? Also, if you want to keep it showing test.domain.com
you will have to use mod_proxy and the [P]
flag. –
Cilo RewriteBase /
breaks the script and makes it display the index page of the root folder. –
Combine */subdomains/test
because of this RewriteRule ^(.*)$ http://domain.com/subdomains/%1/$1 [L,NC,QSA]
. You could replace it by RewriteRule ^(.*)$ http://domain.com/$1 [L,NC,QSA]
–
Onerous http://example.com/subdomains/%1/$1
should be subdomains/%1/$1
. Look here: https://mcmap.net/q/242591/-rewrite-condition-redirects-instead-of-rewriting –
Bullfrog I'm not a mod_rewrite expert and often struggle with it, but I have done this on one of my sites. It might need other flags, etc., depending on your circumstances. I'm using this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$
RewriteCond %{REQUEST_URI} !^/subdomains/subdomain
RewriteRule ^(.*)$ /subdomains/subdomain/$1 [L]
Any other rewrite rules for the rest of the site must go afterwards to prevent them from interfering with your subdomain rewrites.
.htaccess
comes to this page (like I did), besides creating the actual .htaccess
file, for it to become effective, you need to set AllowOverride
in your VirtualHost
settings (https://mcmap.net/q/242593/-htaccess-is-not-working-in-linux-debian-apache2), enable Apache's rewrite
module and then restart Apache (https://mcmap.net/q/73485/-how-to-enable-mod_rewrite-for-apache-2-2) –
Intermix www.
prefix is probably best supported via a redirect than a rewrite. –
Jongjongleur You can use the following rule in .htaccess to rewrite a subdomain to a subfolder:
RewriteEngine On
# If the host is "sub.domain.example"
RewriteCond %{HTTP_HOST} ^sub.domain.example$ [NC]
# Then rewrite any request to /folder
RewriteRule ^((?!folder).*)$ /folder/$1 [NC,L]
Line-by-line explanation:
RewriteEngine on
The line above tells the server to turn on the engine for rewriting URLs.
RewriteCond %{HTTP_HOST} ^sub.domain.example$ [NC]
This line is a condition for the RewriteRule where we match against the HTTP host using a regex pattern. The condition says that if the host is sub.domain.example
then execute the rule.
RewriteRule ^((?!folder).*)$ /folder/$1 [NC,L]
The rule matches http://sub.domain.example/foo
and internally redirects it to http://sub.domain.example/folder/foo
.
Replace sub.domain.example
with your subdomain and folder with name of the folder you want to point your subdomain to.
I had the same problem, and found a detailed explanation in http://www.webmasterworld.com/apache/3163397.htm
My solution (the subdomains contents should be in a folder called sd_subdomain
:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} subdomain\.domain\.example
RewriteCond $1 !^sd_
RewriteRule (.*) /sd_subdomain/$1 [L]
This redirects to the same folder to a subdomain:
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.example$ [NC]
RewriteRule ^(.*)$ http://domain\.example/subdomains/%1
Try to putting this .htaccess file in the subdomain folder:
RewriteEngine On
RewriteRule ^(.*)?$ ./subdomains/sub/$1
It redirects to http://example.org/subdomains/sub/, when you only want it to show http://sub.example.org/.
Redirect subdomain directory:
RewriteCond %{HTTP_HOST} ^([^.]+)\.(archive\.example\.com)$ [NC]
RewriteRule ^ http://%2/%1%{REQUEST_URI} [L,R=301]
For any sub domain request, use this:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.band\.s\.example
RewriteCond %{HTTP_HOST} ^(.*)\.band\.s\.example
RewriteCond %{REQUEST_URI} !^/([a-zA-Z0-9-z\-]+)
RewriteRule ^(.*)$ /%1/$1 [L]
Just make some folder same as sub domain name you need.
Folder must be exist like this: domain.example/sub
for sub.domain.example
.
Edit file .htaccess:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
© 2022 - 2024 — McMap. All rights reserved.
http://test.domain.com
, the URL redirects me tohttp://test.domain.com/subdomains/test/
. Is it possible without showing the/subdomains/test/
at the end? – Combine