htaccess redirect to a separate folder - .htaccess

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.

Related

err_too_many_redirects when trying to redirect non www to www

Inside virtuahost apache conf file
sudo nano /etc/apache2/sites-available/000-default.conf
I tried to put these code inside apache conf file
ServerName example.com
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule ^/(.*)$ http://www.example.com/$1 [L,R=301]
also I tried,
< If "%{HTTP_HOST} != 'YOUR-DOMAIN.com'">
Redirect "/" "http://www.YOUR-DOMAIN.com/"
</If>
also did this
<VirtualHost *:80>
ServerName example.com
Redirect permanent / http://www.example.com/
</VirtualHost>
<VirtualHost *:80>
ServerName www.example.com
# real server configuration
</VirtualHost>
I checked also official apache https://httpd.apache.org/docs/2.4/rewrite/remapping.html#canonicalhost

Apache redirect from https

I installed an SSL Certificate on my server, and https works fine.
Http redirect works fine for example http://login.example.com -> https://example1.com/index.html
But redirect from https not working, for example https://login.example.com -> https://example1.com/index.html.
its just go to the main page of my server https://redirect.example.com
Any suggestions on how to get the redirect from https to work?
Thanks !
<VirtualHost *:80>
RewriteEngine On
ServerName redirect.example.com
RewriteCond %{HTTP_HOST} ^login\.example\.com(.*)
RewriteRule (.*) https://example1.com/index.html [L]
</VirtualHost>
<VirtualHost *:443>
SSLEngine on
SSLCertificateFile /etc/httpd/my.crt
SSLCertificateKeyFile /etc/httpd/my.pem
ServerName redirect.mydomain.com
RewriteEngine On
ServerName redirect.example.com
RewriteCond %{HTTP_HOST} ^login\.example\.com(.*)
RewriteRule (.*) https://example1.com/index.html [L]
</VirtualHost>
Thanks!
I see that you have a duplicate ServerName directive in your SSL host declaration.
That could be causing trouble.
From the Apache documentation:
If you are using name-based virtual hosts, the ServerName inside a section specifies what hostname must appear in the request's Host: header to match this virtual host.
Use server alias for your Rewrite Rule:
ServerAlias login.example.com
you can use multiple ServerAlias for multiple Rewrite Rule
What exactly do you mean by multiple rules ? Multiple domain names/subdomains ?
A plain redirect of http(s)://login.example.com to https://example1.com/index.html should be achieved this way:
<VirtualHost *:80>
ServerName login.example.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^login\.example\.com
RewriteRule (.*) https://example1.com/index.html [L]
</VirtualHost>
<VirtualHost *:443>
SSLEngine on
SSLCertificateFile /etc/httpd/my.crt
SSLCertificateKeyFile /etc/httpd/my.pem
ServerName login.example.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^login\.example\.com
RewriteRule (.*) https://example1.com/index.html [L]
</VirtualHost>
Important: ServerName should match your SSL certificate's Common Name (unless it's a wildcard certificate).
NB: RewriteEngine may be overkill for your purpose since the Apache mod_alias module provides the Redirect directive which may be sufficient here.
From the Apache documentation: When not to use mod_rewrite

Allow dynamic subdomain in url using 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.

force https on subdirectory and redirect non-www traffic to www

So for the past couple hours I've been frustrated with my .htaccess file not working.
I have two subdirectories: secure.mysite.ca and storage.mysite.ca that need to be both ssl encrypted. The sites work when I put https in front of the URL (i.e. https://secure.mysite.ca/ or https://storage.mysite.ca/) but when I put http in front it redirects to my root website (http://mysite.ca/). I want to redirect to the https version of the site when someone tries to go to the unencrypted version.
ALSO I would like to redirect all traffic visiting http://mysite.ca/ to be redirected to http://www.mysite.ca/ (i.e. www in front)
This .htaccess file used to work over a year ago but now it doesn't.
Here is my htaccess file found in the root folder:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mysite\.ca$ [NC]
RewriteRule ^(.*)$ http://www.mysite.ca%{REQUEST_URI} [R=permanent,L]
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{HTTP_HOST} ^(secure|storage)\. [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
Am I doing something wrong? Should I be putting an .htaccess file inside the subdirectory?
EDIT: Here is the virtualhost config file for secure.fixnode.ca, storage.fixnode.ca and www.fixnode.ca. Please let me know what I'm doing wrong
storage.fixnode.ca.conf
<VirtualHost *:443>
DocumentRoot "/var/www/fixnode_website/content/Online Storage"
<Directory "/var/www/fixnode_website/content/Online Storage">
allow from all
Options +Indexes
</Directory>
SSLEngine on
ServerName storage.fixnode.ca
SSLCertificateFile /path/to/cert/storage.crt
SSLCertificateKeyFile /path/to/private key/private.key
</VirtualHost>
secure.fixnode.ca.conf
<VirtualHost *:443>
DocumentRoot "/var/www/fixnode_website/content/Secure Login"
<Directory "/var/www/fixnode_website/content/Secure Login">
allow from all
Options +FollowSymlinks
</Directory>
SSLEngine on
ServerName secure.fixnode.ca
SSLCertificateFile /path/to/cert/cert.crt
SSLCertificateKeyFile /path/to/cert/mykey.key
</VirtualHost>
and finally my virtual host for my root domain www.fixnode.ca.conf
<VirtualHost *:80>
DocumentRoot /var/www/fixnode_website
<Directory "/var/www/fixnode_website">
allow from all
Options +Indexes
</Directory>
ServerAlias fixnode.ca
ServerName www.fixnode.ca
</VirtualHost>
If you have recently made a change to your system, you may have overwritten your apache config file(e.g. httpd.conf or apache2.conf) which may reset the options to default.
You need to place this rule in both the sub-directories of both sub-domains:
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(secure|storage)\. [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

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>

Resources