.htaccess WildCard SSL for sub-domain - .htaccess

I setup .htaccess file for my domain and sub-domain. If I write manually https then both working fine but I want to force so that no one can open without https.
Also I want WWW with my main domain and Sub-domain without WWW. Below is my .htaccess code,
RewriteEngine On
RewriteCond %{HTTP_HOST} ^app.domain.com$
RewriteCond %{REQUEST_URI} !^/app/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /app/$1
RewriteCond %{HTTP_HOST} ^app.domain.com$
RewriteRule ^(/)?$ app/index.php [L]
And Is it possible to redirect user to sub-domain if they type www.domain.com/app/

You can insert these 2 rules:
RewriteEngine On
# force https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
# force www for main site
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
# remove www for app site
RewriteCond %{HTTP_HOST} ^www\.(app\.domain\.com)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{THE_REQUEST} \s/+app[/\s] [NC]
RewriteRule ^ / [R=301,L]
RewriteCond %{HTTP_HOST} ^app.domain.com$
RewriteCond %{REQUEST_URI} !^/app/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /app/$1
RewriteCond %{HTTP_HOST} ^app.domain.com$
RewriteRule ^(/)?$ app/index.php [L]

Related

HTACCESS Stopped working for removing page extensions

I have been using htaccess for performing the following functions:
Redirect to https version
Add WWW. if the request is without www
Remove .php extensions from all pages
Add tailing slash to all pages (eg. /page/)
The code I have is using is:
RewriteRule ^(directory1|directory2)($|/) - [L]
ErrorDocument 404 /404/
#Remove Php extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^([^/]+)/$ $1.php
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
#Add Tailing Slash
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
#Add WWW and https
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteRule (.*) https://www.%1/$1 [R=301,L]
#SEO INDEX FIX
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ https://www.example.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^.*/index.html
RewriteRule ^(.*)index.html$ https://www.example.com/$1 [R=301,L]
Everything used to work fine before but now after I upgraded to php 7.4 if I try to access page using .php extension its not removing .php and adding /.
Please help me correct the htaccess code..
Thanks!
You can use this htaccess file :
RewriteEngine On
#Add WWW and https
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule (.*) https://www.%1/$1 [R=301,L]
#Add a trailing slash to pages
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !/$ %{REQUEST_URI}/ [R,L]
#Remove .PHP from URLs
#The rule bellow redirects URLs to remove .PHP (file.php =>file)
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1/ [L,R]
#The rule bellow maps non PHP URLs to PHP
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ /$1.php [L]
Make sure to clear your browser cache or use a different browser to test these rules.

redirect old url to new url of same domain using htaccess

I would like to redirect https://www.example.com/page/jobseekers to https://www.example.com/jobseekers
I used
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
Redirect /page/jobseekers /jobseekers
But its redirecting into https://www.example.com/jobseekers?page/jobseekers.
Place this rule just below RewriteEngine On line:
RewriteEngine On
RewriteRule ^page/(jobseekers)/?$ /$1? [L,NC,R=301]
# other rules go below
Make sure to test in a new browser or clear browser cachhe before testing.

Redirect www to https://www in .htaccess

So far I have .htaccess file which is able to redirect correctly from (domain.com) to (https://www.domain.com) but not working if I go to www.domain.com and it must redirect (www.domain.com) to (https://www.domain.com)
I need (https://www) always before my site url
my .htaccess file
RewriteEngine On
RewriteCond !{HTTPS} off
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Replace your first 2 redirect rules with this rule:
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,R=301,NE]
Make sure to clear your browser cache before testing this.

Force SSL for eCommerce on Site

I have a series of subdomains like the following on my site:
site.website.com/shop
site2.website.com/shop
For all of my domains where I have my store I'd like this to be forced over SSL. Is there a wildcard for subdomains? How can I do this?
You can do something like this with your .htaccess:
RewriteEngine On
# force https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
# force www for main site
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
# remove www for app site
RewriteCond %{HTTP_HOST} ^www\.(app\.domain\.com)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{THE_REQUEST} \s/+app[/\s] [NC]
RewriteRule ^ / [R=301,L]
RewriteCond %{HTTP_HOST} ^app.domain.com$
RewriteCond %{REQUEST_URI} !^/app/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /app/$1
RewriteCond %{HTTP_HOST} ^app.domain.com$
RewriteRule ^(/)?$ app/index.php [L]

Force https:// for some URLs, and force www. on all

I'm trying to set up an SSL for a site using Codeigniter.
I need to to force https:// on certain pages (such as login, basket etc).
What I have so far, which forces www. successfully, and removes the index.php from the URL for cleaner results with Codeigniter:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1
RewriteCond %{HTTP_HOST} ^mydomain\.com [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Keep your redirect rules before rewrite ones:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^mydomain\.com [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{SERVER_PORT} !=443 [OR]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{THE_REQUEST} /(login|basket|shop)
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

Resources