I have created a virtual host www.maindomain.com for my site. I want to allow dynamic sub domains in the url using htaccess i.e test.maindomain.com, abc.maindomain.com, etc should be allowed. My site folder structure is www/test/index.php.
Here is my current htaccess code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{HTTP_HOST} !^www\.maindomain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([a-z]+)\.maindomain\.com$ [NC]
RewriteRule .? index.php?url=%1&path=%{REQUEST_URI} [L,QSA]
</IfModule>
I get Server not found and could not connect error messages.
Can anyone suggest me a good solution for this issue?
Thanks in advance!
If your server is online:
You need a Wildcard DNS record plus a Wildcard serverAlias in your VirtualHost:
<VirtualHost *:80>
ServerAdmin admin#example.local
DocumentRoot "/my_server/htdocs"
ServerName example.local
ServerAlias *.example.local
# [...]
</VirtualHost>
Finally you will be able to catch those subdomains in your .htaccess file.
I need to redirect subfolder to subdomain using htaccess
For example:
redirect www.example.com/test (also http://, https://, http://www, https://www)
to
https://user.example.com
how do I do it?
also, how it should be if I need to redirect it to https://user.example.com/test
You can put this code in your htaccess (which has to be in root folder)
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.com$ [NC]
RewriteRule ^test/?$ https://user.example.com/test [R=301,L]
Don't forget to enable mod_rewrite if it's not yet done
Look at original answer at
Can you do a different for blog.example.com?
If you can, just put your Redirect config there:
<VirtualHost *:80>
ServerName blog.example.com
Redirect / http://example.com/blog/
</VirtualHost>
If for some reason you still need to piggy-back on the other vhost, or need to handle all subdomains and not just "blog", then use mod_rewrite:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com$
RewriteRule ^/(.*)$ http://example.com/%1/$1 [R=301,L]
I'm using the following in my .htaccess file and works at main level but not subfolders. So the problem is http://mydoamin.com/folder/file.php redirects to the wrong location http://mydomain.com/file.php and the subfolder is lost.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^(www\.)?mydomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]
If you have access to the apache config create another vhost site
<VirtualHost *:80>
ServerName mydomain.com
ServerAlias anotherdomain.com anotherdomain.org ...
RedirectPermanent / http://www.mydomain.com/
</VirtualHost>
Environment Centos with apache
Trying to setup automatic redirection from http to https
From manage.mydomain.com --- To ---> https://manage.mydomain.com
I have tried adding the following to my httpd.conf but it didn't work
RewriteEngine on
ReWriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]
Any ideas?
I have actually followed this example and it worked for me :)
NameVirtualHost *:80
<VirtualHost *:80>
ServerName mysite.example.com
Redirect permanent / https://mysite.example.com/
</VirtualHost>
<VirtualHost _default_:443>
ServerName mysite.example.com
DocumentRoot /usr/local/apache2/htdocs
SSLEngine On
# etc...
</VirtualHost>
Then do:
/etc/init.d/httpd restart
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}
http://www.sslshopper.com/apache-redirect-http-to-https.html
or
http://www.cyberciti.biz/tips/howto-apache-force-https-secure-connections.html
Searched for apache redirect http to https and landed here. This is what i did on ubuntu:
1) Enable modules
sudo a2enmod rewrite
sudo a2enmod ssl
2) Edit your site config
Edit file
/etc/apache2/sites-available/000-default.conf
Content should be:
<VirtualHost *:80>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</VirtualHost>
<VirtualHost *:443>
SSLEngine on
SSLCertificateFile <path to your crt file>
SSLCertificateKeyFile <path to your private key file>
# Rest of your site config
# ...
</VirtualHost>
Note that the SSL module requires certificate. you will need to specify existing one (if you bought one) or to generate a self-signed certificate by yourself.
3) Restart apache2
sudo service apache2 restart
Using mod_rewrite is not the recommended way instead use virtual host and redirect.
In case, if you are inclined to do using mod_rewrite:
RewriteEngine On
# This will enable the Rewrite capabilities
RewriteCond %{HTTPS} !=on
# This checks to make sure the connection is not already HTTPS
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
# This rule will redirect users from their original location, to the same
location but using HTTPS.
# i.e. http://www.example.com/foo/ to https://www.example.com/foo/
# The leading slash is made optional so that this will work either in
# httpd.conf or .htaccess context
Reference: Httpd Wiki - RewriteHTTPToHTTPS
If you are looking for a 301 Permanent Redirect, then redirect flag should be as,
R=301
so the RewriteRule will be like,
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]
Actually, your topic is belongs on https://serverfault.com/ but you can still try to check these .htaccess directives:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*) https://%{HTTP_HOST}/$1
If you have Apache2.4 check 000-default.conf - remove DocumentRoot and add
Redirect permanent / https://[your-domain]/
I needed this for something as simple as redirecting all http traffic from the default apache home page on my server to one served over https.
Since I'm still quite green when it comes to configuring apache, I prefer to avoid using mod_rewrite directly and instead went for something simpler like this:
<VirtualHost *:80>
<Location "/">
Redirect permanent "https://%{HTTP_HOST}%{REQUEST_URI}"
</Location>
</VirtualHost>
<VirtualHost *:443>
DocumentRoot "/var/www/html"
SSLEngine on
...
</VirtualHost>
I like this because it allowed me to use apache variables, that way I didn't have to specify the actual host name since it's just an IP address without an associated domain name.
References:
https://stackoverflow.com/a/40291044/2089675
Server version: Apache/2.4.29 (Ubuntu)
After long search on the web and in the official documentation of apache, the only solution that worked for me came from /usr/share/doc/apache2/README.Debian.gz
To enable SSL, type (as user root):
a2ensite default-ssl
a2enmod ssl
In the file /etc/apache2/sites-available/000-default.conf add the
Redirect "/" "https://sub.domain.com/"
<VirtualHost *:80>
#ServerName www.example.com
DocumentRoot /var/www/owncloud
Redirect "/" "https://sub.domain.com/"
That's it.
P.S: If you want to read the manual without extracting:
gunzip -cd /usr/share/doc/apache2/README.Debian.gz
This code work for me.
# ----------port 80----------
RewriteEngine on
# redirect http non-www to https www
RewriteCond %{HTTPS} off
RewriteCond %{SERVER_NAME} =example.com
RewriteRule ^ https://www.%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
# redirect http www to https www
RewriteCond %{HTTPS} off
RewriteCond %{SERVER_NAME} =www.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
# ----------port 443----------
RewriteEngine on
# redirect https non-www to https www
RewriteCond %{SERVER_NAME} !^www\.(.*)$ [NC]
RewriteRule ^ https://www.%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
Please try this one in apache Virtualhosting configuration
and then reload apache service
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}
for me this worked
RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
This worked for me:
RewriteCond %{HTTPS} =off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [QSA,L,R=301]
i want to redirect my.example.com to example.com/my . I am using drupal . in this website(http://www.easymodrewrite.com/example-subdomains) , i found these steps
in dns,point *.domain.com to my ip - DONE
in server httpd.conf , ServerAlias www.domain.com domain.com to ServerAlias www.domain.com domain.com *.domain.com - Not done ,as i am on shared server
in .htaccess , add following
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).domain.com [NC]
RewriteRule (.*) %2/$1 [L]
</IfModule>
its not working .. is it important to edit httpd.conf or am i mssing something?
its not working .. is it important to edit httpd.conf or am i mssing something?
Yes, otherwise the webserver won't know to accept requests for hosts other than what the server is set to. There may be some setting in cpanel or whatever your host uses to configure the server that allows you to set the server host to *.domain.com
Otherwise, the rewrite rules should be fine, but there won't be any redirecting, just internal rewrite. If you want it to redirect, change the rule line to:
RewriteRule (.*) http://www.domain.com/%2/$1 [L,R]