301 redirect multiple domains to single domain - .htaccess

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>

Related

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.

htaccess rewrite subdomain to subfolder

I am trying to achieve getting subdomains pointed to subfolders so for example
http://{subdomain}.example.com points to http://example.com/{subdomain} without changing the URL in the address bar.
I currently have this code:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com
RewriteRule ^(.*)$ http://example.com/%1 [L]
Which seems to work, but it changes my URL bar aswell
I knnow there are multiple questions about this, but none fitted my awnser.
Try using virtualhosts instead, edit your httpd-vhosts.conf and add this:
<VirtualHost *:80>
ServerAlias *.yourdomain.com #wildcard catch all
VirtualDocumentRoot /opt/lampp/htdocs/%1
UseCanonicalName Off
<Directory "opt/lampp/htdocs">
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
You can use this lookahead based rule in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^.*\.example\.com$ [NC]
RewriteCond %{HTTP_HOST}:%{REQUEST_URI} ^([^.]+)\.[^:]+:(?!/\1/).*$
RewriteRule ^ %1%{REQUEST_URI} [L]

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]

How to redirect directory to sub-domain, and www sub-domain to non-www sub-domain?

NOTE: Using <VirtualHost> parameter only.
How do I redirect a directory (say example.com/groups/) to a sub-domain (say groups.example.com), and also the www of that sub-domain (www.groups.example.com) to the non-www URL of that sub-domain (groups.example.com)?
Precisely (⇒ stands for 'should 301 redirect to'):
example.com/groups/ ⇒ groups.example.com
www.groups.example.com ⇒ groups.example.com
I have read that using <VirtualHost> is better than all those redirection rules that are usually used (i.e. that using redirection engine is pretty heavy on disk i/o). So, I would like to have the above problem solved using <VirtualHost> in .htaccess / httpd.conf if possible. Thanks!
Something like this should work
<VirtualHost *:80>
ServerName example.com
ServerAlias groups.example.com www.groups.example.com
RewriteEngine on
# example.com/groups/ -> groups.example.com
RewriteCond %{SERVER_NAME} ^example\.com$
RewriteRule ^/(\w+)/?$ http://$1.example.com [R=301,L]
# www.groups.example.com -> groups.example.com
RewriteCond %{SERVER_NAME} ^www\.(\w+)\.example\.com$
RewriteRule ^/(.*)$ http://%1.example.com/$1 [R=301,L]
</VirtualHost>
or specifically for groups
<VirtualHost *:80>
ServerName example.com
ServerAlias groups.example.com www.groups.example.com
RewriteEngine on
# example.com/groups/ -> groups.example.com
RewriteCond %{SERVER_NAME} ^example\.com$
RewriteRule ^/groups/?$ http://groups.example.com [R=301,L]
# www.groups.example.com -> groups.example.com
RewriteCond %{SERVER_NAME} ^www\.groups\.example\.com$
RewriteRule ^/(.*)$ http://groups.example.com/$1 [R=301,L]
</VirtualHost>

.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