htaccess subdomain - .htaccess

Howto make this with htacess:
subdomain.domain.com -> domain.com/subdomain (no redirect on client side)
domain.com/subdomain -> subdomain.domain.com (redirect)
I make firs redirect:
RewriteRule ^subdomain/ - [L]
RewriteCond %{HTTP_HOST} ^subdomain.domain.com [NC]
RewriteRule (.*) subdomain/$1 [L]
How is it inserted in the form htacess:
AddDefaultCharset utf-8
DirectoryIndex index.php index.html index.htm
Options All -Indexes
ErrorDocument 404 /404.html
ErrorDocument 403 /404.html
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.com
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !\.(png|jpg|jpeg|gif)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)$ index.php/$1 [QSA]

This is a wildcard based solution, so it sould work for any number of subdomains.
This will redirect domain.com/foo/bar to foo.domain.com/bar:
RewriteCond %{HTTP_HOST} ^domain.com$ [NC]
RewriteRule ([^/]+)(/.*|$) $1.domain.com/$2 [R=302]
This will handle (internal rewrite) the virtual hosts:
RewriteCond %{HTTP_HOST} ^(.*).domain.com$ [NC]
RewriteRule (.*) %1/$1 [L]
You might consider using 301 (permanent redirect) instead of the 302.
I strongly suggest you have a VirtualHost for your main site domain.com or www.domain.com and a separate one for handling the virtual subdomains.
For only a certain subdomain:
RewriteCond %{HTTP_HOST} ^domain.com$ [NC]
RewriteRule (subdomain)(/.*|$) $1.domain.com/$2 [R=302,L]
RewriteCond %{HTTP_HOST} ^(subdomain).domain.com$ [NC]
RewriteRule (.*) %1/$1 [L]

Related

Trying to figure out this redirection in .htaccess

This is the .htaccess file for one of the sites I am working on. My apache virtualhost is set to redirect http to https automatically. I am running into a problem when I go to my http://www version. It adds index.php in between. Can you see what is wrong here?
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{THE_REQUEST} ^.*/\.
RewriteRule ^(.*)$ - [R=404]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
ErrorDocument 404 /404.html

htaccess: URL with any URI not redirecting to https://www

Currently, this is my .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Redirect to HTTPS
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule !/status https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
# Send would-be 404 requests to Craft
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
RewriteRule (.+) index.php?p=$1 [QSA,L]
# Custom Redirects
Redirect /investors/reporting/estma /investors/investor-reporting/financial-information#estma
RewriteCond %{HTTP_HOST} ^portal\.arcresources\.com [NC]
RewriteRule ^(.*) https://www\.arcresources\.com/portals [L,R=301]
</IfModule>
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
</IfModule>
The redirect works fine if you just type the domain, so arcresources.com properly redirects to https://www.arcresources.com. It also works if you include www and any URI, so www.arcresources.com/investors redirects correctly as well.
The problem is typing just the domain + a URI, so arcresources.com/investors redirects to only https://arcresources.com/investors (no www.), resulting in a connection refusal.
I'm sure I'm missing something simple, but all my attempts so far have either added double "www"s or resulted in too many redirects.
Thanks in advance :)
Try it like this:
RewriteEngine On
RewriteBase /
# Redirect HTTP with www to HTTPS with www
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Redirect HTTP without www to HTTPS with www
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Redirect HTTPS without www to HTTPS with www
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Send would-be 404 requests to Craft
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
#RewriteCond %{REQUEST_URI} !index.php [NC]
RewriteRule (.+) index.php?p=$1 [QSA,L]

Redirect multiple domains to https only

I have three domains:
example.com
example.net
domain2.example
I want all domains (www/non-www/HTTPS/non-HTTPS traffic) to redirect to https://www.example.com.
I know I need to redirect example.net and domain2.example (www/non-www and HTTPS) first to example.com and then https://www.example.com (SSL certificate is only for example.com and www.example.com).
.htaccess (whole file):
RewriteEngine on
RewriteBase /
RewriteCond !{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.)?example.com [NC]
RewriteRule ^/?$ "https\:\/\/www\.example\.com" [R=301,L]
RewriteCond !{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.)?example.net [NC,OR]
RewriteCond %{HTTP_HOST} ^(www\.)?domain2.example [NC]
RewriteRule ^/?$ "http\:\/\/www\.domain1\.com" [R=301,L]
RewriteCond !{HTTPS} on
RewriteCond %{HTTP_HOST} ^(www\.)?example.com [NC]
RewriteRule ^/?$ "https\:\/\/www\.example\.com" [R=301,L]
RewriteCond !{HTTPS} on
RewriteCond %{HTTP_HOST} ^(www\.)?example.net [NC,OR]
RewriteCond %{HTTP_HOST} ^(www\.)?domain2.example [NC]
RewriteRule ^/?$ "http\:\/\/www\.example\.com" [R=301,L]
# disable directory listing
Options -Indexes
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Only example.com redirect rules seems to be working rigth, but example.net and domain2.example redirects does not work. Is this possible to get work?
This should be enough:
RewriteCond !{HTTPS} on [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L]
Keep in mind that in order to respond to https:// requests for other domains, you need to have the certificate for that domain name (you need it even if you only want to do the redirect!).

Redirect www to https non www

I am trying to redirect my site from www to https non www. I have this code in htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([0-9a-zA-Z].+)$ /page.php?title=$1 [L]
ErrorDocument 404 /404.php
Can you suggest changes in the .htaccess file ?
Try this:
RewriteEngine On
Match your URL with www. and rewrite to it SSL https without www.
RewriteCond %{HTTP_HOST} ^(www\.)(.*) [NC]
RewriteRule (.*) https://%2%{REQUEST_URI} [L,R=301]
Match your URL that does not have SSL https
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^(www\.)(.*) [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

A simple mod re-write rule doesnt work with htaccess

I want the user to be redirected whenever he reaches my subdomain
Here is what is inside my htaccess:
RewriteEngine On
RewriteRule ^http://smale.deals.com/(.*) http://traual.deals.com/$1 [R=301,L]
RewriteRule ^http://deals.com/smale/(.*) http://deals.com/traual/$1 [R=301,L]
But no redirect happens. why?
I also have got this in my root htaccess:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_USER_AGENT} (Android|iPhone|iPod|Blackberry) [NC]
RewriteCond %{REQUEST_URI} !^/mobile [NC]
RewriteRule ^(.*)/?$ mobile/$1 [L]
You cannot include the protocol and domain in RewriteRule. Those need to be accounted for in RewriteCond:
RewriteEngine On
# Rewrite requests to smale.deals.com to traual.deals.com
RewriteCond %{HTTP_HOST} ^smale\.deals\.com$ [NC]
RewriteRule (.*) http://traual.deals.com/$1 [R=301,L]
# For deals.com...
RewriteCond %{HTTP_HOST} ^deals\.com$ [NC]
# Rewrite requests to smale/ to deals.com/traual/
RewriteRule ^smale/(.*) http://deals.com/traual/$1 [R=301,L]

Resources