I'm trying to accomplish the following:
non-http force to https - works
www force to non-www - works
website loaded from subfolder (/web) - works
test.example.com load different subfolder (/test) - does not work
4, Does not work, the condition is met to go to /web. Can't understand how to change this into /test
the .htaccess code I use:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^test.example.com$
RewriteRule ^(.*)$ /test/$1 [L,NC,QSA]
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^example.com$ [NC,OR]
RewriteRule ^(.*)$ /example/web/$1 [L,NC,QSA]
With your shown samples please try following htaccess rules file here. Please make sure to clear your browser cache before testing your URLs. In case you have further more rules(apart from shown ones) then make sure these Rules are before those rules.
RewriteEngine On
##Apply https to uris here..
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
##Apply non-www to uris here..
RewriteCond %{HTTP_HOST} ^(?:www\.)(example\.com)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R=301]
##Apply test to all uris here..
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/test [NC]
RewriteRule ^(.*)/?$ example/web/$1 [L,NC,QSA]
##Apply test to all uris here..
RewriteCond %{HTTP_HOST} ^test\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/test [NC]
RewriteRule ^(.*)/?$ www.example.com/test/$1 [L,NC,QSA]
Related
Im trying to redirect several urls with 3 parameters to different static urls with .htaccess but nothing working.
1.
http://olddomain.com/index.php?id_category=28&controller=category&id_lang=2
to
https://newdomain.com/page1/
http://olddomain.com/index.php?id_category=30&controller=category&id_lang=2
to
https://newdomain.com/page2/
http://olddomain.com/index.php
to
https://newdomain.com
I tried the below code but http://olddomain.com/index.php not going to https://newdomain.com :
RewriteCond %{HTTP_HOST} ^olddomain.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com [NC]
RewriteRule ^(.*)$ https://newdomain.com/$1 [L,R=301,NC]
RewriteCond %{QUERY_STRING} ^id_category=28&controller=category&id_lang=2$
RewriteRule ^index.php$ https://newdomain.com/page1/? [R=301,L]
RewriteCond %{QUERY_STRING} ^id_category=30&controller=category&id_lang=2$
RewriteRule ^index.php$ https://newdomain.com/page2/? [R=301,L]
You need to have specific longer matches first and then have rules to remove index.php or domain redirect:
RewriteEngine On
# specific redirects with index.php as optional match
RewriteCond %{QUERY_STRING} ^id_category=28&controller=category&id_lang=2$ [NC]
RewriteRule ^(index\.php)?$ https://newdomain.com/page1/? [R=301,L,NC]
RewriteCond %{QUERY_STRING} ^id_category=30&controller=category&id_lang=2$ [NC]
RewriteRule ^(index\.php)?$ https://newdomain.com/page2/? [R=301,L,NC]
# remove index.php and redirect to newdmain
RewriteCond %{HTTP_HOST} ^(?:www\.)?olddomain\.com$ [NC]
RewriteRule ^(?:index\.php/?)?(.*)$ https://newdomain.com/$1 [L,R=301,NC,NE]
Make sure to clear your browser cache before testing this change.
In case you are taking page's id from id_lang= variable then please try following rules. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##Rules to redirect to link: https://newdomain.com/page1/ here.
RewriteCond %{HTTP_HOST} ^(?:www\.)?olddomain\.com$ [NC]
RewriteCond %{THE_REQUEST} \s/index\.php\?id_category=28&controller=category&id_lang=(\d+)\s [NC]
RewriteRule ^ https://newdomain.com/page%1/? [NE,R=301,L]
##Rules to redirect https://newdomain.com/ here.
RewriteCond %{HTTP_HOST} ^(?:www\.)?olddomain\.com$ [NC]
RewriteCond %{THE_REQUEST} \s/index\.php\s [NC]
RewriteRule ^ https://newdomain.com [NE,R=301,L]
I'm running into an issue where by my subdomains are incorrectly getting www appended to them via a htaccess rewrite rule...
My folder structure is as follows:
/public_html/index.html (A maintenance page just in case)
/public_html/.htaccess
/public_html/websitename
/public_html/subdomain
/public_html/testsite
/public_html/clone
My /public_html/ that's located in my .htaccess looks as follows
# Force HTTPS & WWW
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} websitename.com$ [NC]
RewriteRule ^(.*)$ /websitename/$1 [L]
RewriteEngine On
RewriteCond %{HTTP_HOST} subdomain.website.com$ [NC]
RewriteRule ^(.*)$ /subdomain/$1 [L]
</IfModule>
Which works perfectly for websitename.com, which forces the URL to be rewrote to https://www.websitename.com
However it makes my subdomains get incorrectly rewrote to https://www.subdomain.websitename.com when it should be https://subdomain.websitename.com
I don't want to have to put the forcing of HTTPS & WWW in the individual website folders... rather, I'm looking for a solution to make subdomain exempt from the rewrite rule.
I tried adding the following condition but it didn't help:
RewriteCond %{HTTP_HOST} subdomain.websitename.com$ [NC]
Anyone have any idea what I can do to get around this issue?
Remove:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]
And add:
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
For multiple domains you can use:
RewriteCond %{HTTP_HOST} ^([^.]+)\.([a-z]{2,4})$ [NC]
RewriteRule ^ https://www.%1.%2%{REQUEST_URI} [R=301,L]
I have this code in my .htaccess file which does as the comments say:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
Options -Indexes
# redirect all traffic to correct domain
RewriteCond %{HTTP_HOST} ^itl\.|^(www\.)?(integratelecom|integra|integratelecommunications)\b [NC]
RewriteRule ^ https://www.example.net%{REQUEST_URI} [L,R=301,NE]
# redirect admin./ssladmin. sub domain to the correct folder
RewriteCond %{HTTP_HOST} (admin|ssladmin)\.itl\.uk\.net$ [NC]
RewriteRule !^admin/system/ admin/system%{REQUEST_URI} [L,NC]
# redirect subdomain.X to subdomain.example.net
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.(?!itl\.)[^.]+\. [NC]
RewriteRule ^ https://%1.example.net%{REQUEST_URI} [L,NE,R=302]
# map subdomains to correct folder
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.example\.net$ [NC]
RewriteRule !^subdomains/ subdomains/%1%{REQUEST_URI} [L,NC]
RewriteRule ^(subdomains/|admin/|index\.php$) - [L,NC]
# stop external linking to website but allow listed domains below
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?google.co.uk [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?google.com [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(.*\.)?itl.uk.net [NC]
#RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.|admin\.|ssladmin\.)?example.net [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ – [NC,F,L]
#######################################
############## MAIN SITE ##############
#######################################
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(blog)/(post|tags|category)/([\w-]+)/?$ index.php?id=$1&type=$2&unique=$3 [QSA,NC,L]
RewriteRule ^(blog)/(archives)/([0-9]{4})/([0-9]{2})?$ index.php?id=$1&type=$2&year=$3&month=$4 [QSA,NC,L]
RewriteRule ^(support/knowledgebase)/(article|category|search)/([\w-]+)?$ index.php?id=$1&type=$2&unique=$3 [QSA,NC,L]
RewriteRule ^([a-zA-Z0-9-/_]+)/?$ index.php?id=$1 [L,QSA]
All of the above is working fine, but i need to force every domain and subdomain to use HTTPS, I have tried adding in:
RewriteCond %{HTTPS} off %HTTP_HOST [NC]
RewriteRule ^ https://%1.domain.net%{REQUEST_URI} [L,NE,R=302]
But that keeps returning an Internal Server Error
I also tried these two lines, but that adds %25 to the end of the URL
#RewriteCond %{HTTPS} off
#RewriteRule (.*) https://%{SERVER_NAME}/%$1 [R,L]
To force HTTPs you can use:
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
It basically says, if HTTPs is not equal to ON, then it will force the website to display using SSL.
Make sure you clear your cache before testing this.
Those are the lines of concern currently in my .htaccess file,
note that i have two domains pointing to the same site:
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule .* http://www.example.com/ [L,R]
RewriteCond %{HTTP_HOST} ^example2\.com$ [NC]
RewriteRule .* http://www.example.com/ [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.example2\.com$ [NC]
RewriteRule .* http://www.example.com/ [L,R=301]
Now I need to convert my site to SSL, I was instructed to use the following
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
I think that this code misses my other domain and the www rewrite part,
how to enable SSL while accounting for my other domain and also for the WWW part?
You can use:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^(www.)?example2\.com$ [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,L,R=301]
I want to remove the www from https url by .htaccess.
for example
i want to change
https://www.example.com/ to https://example.com/
I have tried many rules
RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ https://example.come/$1 [L]
and
RewriteCond %{SERVER_PORT} ^443
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule (.*) https://example.com$1 [L,R,QSA]
and
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
But nothing work. Please help me
Try:
RewriteCond %{HTTP_HOST} ^www\.(.+)
RewriteCond %{HTTPS}s/%1 ^(on(s)|offs)/(.+)
RewriteRule ^ http%2://%3%{REQUEST_URI} [L,R=301]
Please make sure if you are using X-Forwarding then try this one:
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://example.com/$1 [R,L]