Allow dynamic subdomain in url using htaccess - .htaccess

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.

Related

htaccess redirect to a separate folder

Server is CentoS 7 with Apache
I have 2 folder under var/www/html/ which are
app/app_name/public (which contain laravel 4.2)
landing_page (which contain a simple index.html and a create.php file for subscription)
When you are accessing
domain.com -- this will open the landing_page/index.html
I would like to make it that when domain.com/beta is open, it will open up the request to the laravel application like:
var/www/html/app/app_name/public
I have tried virtual host which is
<VirtualHost *:80>
ServerName www.domain.com
ServerAlias domain.com
DocumentRoot /var/www/html/landing_page
ErrorLog /var/www/html/landing_page/error.log
CustomLog /var/www/html/landing_page/requests.log combined
Redirect /beta http://domain.com/app/app_name/public
</VirtualHost>
And .htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Not sure what is the easiest but as long as it can make it work, should be good. Thanks.

301 redirect multiple domains to single domain

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>

How to automatically redirect HTTP to HTTPS on Apache servers?

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]

htaccess - want to redirect my.example.com to example.com/my

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]

.htaccess mod_rewrite works only with www

I've got a .htaccess file in a subdirectory of a site I'm working on. It works as I want when the URL begins with www, but if www is missing, it doesn't work.
RewriteEngine On
RewriteBase /media
RewriteRule ^([a-zA-Z0-9_\s\-]+)/?$ index.php?a=$1 [L]
RewriteRule ^([a-zA-Z0-9_\s\-]+)/([a-zA-Z0-9_\s\-]+)$ index.php?a=$1&b=$2 [L]
If I request http://www.mysite.com/media/test, it works, but http://mysite.com/media/test doesn't. What am I missing?
This has nothing to do with your rewrite rules, this probably has something to do with the way your DNS is configured.
Try to ping both domains: with and without 'www'.
Here's what I do: I configure my DNS so that everything with or without www is redirected to the same PC:
Then I handle everything through vhost configuration:
<VirtualHost *>
ServerAdmin webmaster#livrepizzas.fr
DocumentRoot "/web/htdocs/olivier/livrepizzas/dev/website"
ServerName livrepizzas.fr
ServerAlias *.livrepizzas.fr
...
...
...
</VirtualHost>
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.website.com [NC]
RewriteRule ^(.*) http://website.com%{REQUEST_URI} [R=permanent,QSA,L]
Try that

Resources