All the possible ways of writing a domain name should be rewritten to https and non-www. i.e.
example.com => https://example.com
www.example.com => https://example.com
http://example.com => https://example.com
http://www.example.com => https://example.com
https://www.example.com => https://example.com
Those rules also applies for all of the sub domains (https and non-www).
The main site should be rewritten to a folder called "main". At the moment I have one sub domain which should be rewritten to a folder called "sub".
So I got it working trough .htaccess but this code is a bit messy and I'm pretty sure there is a nicer/cleaner way to achieve what I want.
Could someone help me out to improve the following .htaccess?
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.
RewriteCond http%1://%{HTTP_HOST} ^(https?://)(www\.)?(.+)$
RewriteRule ^ %1%3%{REQUEST_URI} [R,L]
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/main/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /sub/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ /sub/$1
# Force https
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
The URL of the main domain should be like: https://example.com and sub domain should be like https://sub.example.com
You can use these rules
RewriteEngine on
#redirect main site to https non www
RewriteCond %{HTTP_HOST} ^www\. [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(mainsite\.com)$
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R]
#rewrite mainsite to /mainsite folder
RewriteCond %{HTTP_HOST} ^(www\.)?mainsite\.com$
RewriteCond %{REQUEST_URI} !^/mainsitefolder
RewriteRule ^ /mainsitefolder%{REQUEST_URI} [L]
#redirect subdomain to https and subfolder
RewriteCond %{HTTP_HOST} ^sub.domain.com$ [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(sub\.domain\.com)
RewriteRule ^ https://sub.domain.com%{REQUEST_URI} [NE,L,R]
#rewrite subdomain to subfolder
RewriteCond %{HTTP_HOST} ^sub.domain.com$
RewriteCond %{REQUEST_URI} !^/subdomainfolder
RewriteRule ^ /subdomainfolder%{REQUEST_URI} [L]
Related
I need to automatically add www if thereĀ“s no subdomain in the url
https://example.com => https://www.example.com
http://test.example.com => http://test.example.com (no change)
it also will be nice to make it work for both http and https.
this is what I have so far
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
You can use this :
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+\.[^.]+)$
RewriteRule ^ https://www.%2%{REQUEST_URI} [NE,L,R]
I have
sub.domain.com and want it to be redirected to domain.com
But there must not be redirects from e.g. sub.domain.com/folder/ or sub.domain.com/folder/index.html
Everything i've tried does not work...
For example
RewriteEngine on
RewriteCond %{HTTP_HOST} sub.domain\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
Rewriterule ^(.*)$ http://www.domain.com/ [L,R=301]
You can use this :
RewriteEngine on
RewriteCond %{HTTP_HOST} sub.domain\.com [NC]
RewriteCond %{REQUEST_URI} !^/folder/?$
RewriteCond %{REQUEST_URI} !^/folder/index\.html$
Rewriterule ^(.*)$ http://www.domain.com/ [L,R=301]
I have a cpanel account with maindomain.com and many other domains inside it.I used below code to Redirect all url to non-www HTTPS and put the .htaccess in root folder.
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{ENV:HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301,NE]
But this is affecting all other domains too in ftp account , how to make this work only for the domain i want..For example maindomain.com
You can do this in a single rule like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?maindomain\.com$ [NC]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://maindomain.com%{REQUEST_URI} [L,R=301,NE]
First I directed my /example folder to main domain. Now, I'd like to redirect some of the pages to HTTPS. Pls, anyone could advice me about the code:
# com upload yonlendirme
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteCond %{REQUEST_URI} !^/example/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /example/$1
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteRule ^(/)?$ example/index.php [L]
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
#redirect www.mydomain.com to mydomain.com (or any other subdomain)
RewriteCond %{HTTP_HOST} !^domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]
#force https for certain pages
RewriteCond %{HTTPS} !=on
RewriteRule ^(index\.php?route=account/register|index\.php?route=account/login|index\.php?route=account/account|index\.php?route=checkout/checkout)$ https://%{HTTP_HOST}%{REQUEST_URI}[L,R]
You can't match against the query string (everything after the ?) in a rewrite rule. You'll need to match against the %{QUERY_STRING} variable in a rewrite condition:
RewriteCond %{HTTPS} off
RewriteCond %{QUERY_STRING} ^route=(account/register|account/login|account/account|checkout/checkout)
RewriteRule ^index.php$ https://%{HTTP_HOST}%{REQUEST_URI}[L,R]
You may also want to move that rule up to the top of your htaccess file so the routing to the example folder doesn't interfere with the redirects.
I have a domain, www.example.com and subdomain test.example.com. When i call test.example.com i need to redirect this to www.example.com/search/property/test_state without changing the url from test.example.com. so the url should look like test.example.com only.
I did this with the .htaccess code like,
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} !^webmail\.example\.com
RewriteCond %{HTTP_HOST} !^m\.example\.com
RewriteCond %{HTTP_HOST} ([^.]+)\.example\.com
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteRule ^$ /search/property/%1_state [P]
Now after this when i access any urls from the subdomain like test.example.com/content/sell_your_home it should not change the url to main domain.
currently when i access this it is redirecting to main domain. any ideas?
If the test subdomain has the same server root (document root) as the main domain, then you don't need the P flag:
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} !^webmail\.example\.com
RewriteCond %{HTTP_HOST} !^m\.example\.com
RewriteCond %{HTTP_HOST} ([^.]+)\.example\.com
RewriteRule ^$ /search/property/%1_state/ [L]
Otherwise, you need the full URL:
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} !^webmail\.example\.com
RewriteCond %{HTTP_HOST} !^m\.example\.com
RewriteCond %{HTTP_HOST} ([^.]+)\.example\.com
RewriteRule ^$ http://www.example.com/search/property/%1_state/$1 [L,P]
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} !^webmail\.example\.com
RewriteCond %{HTTP_HOST} !^m\.example\.com
RewriteCond %{HTTP_HOST} ([^.]+)\.example\.com
RewriteRule ^(.*)$ http://www.example.com/$1 [L,P]