HTACCESS Stopped working for removing page extensions - .htaccess

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.

Related

Add trailing slash at dynamic .htaccess

I have a site with a big .htaccess with a lot of dynamic rules, everything works fine, but unfortunately, Google is duplicating my URLs, considering the same URL with trailing slash and without... I will paste the code of my .htaccess if someone could help me to enforce adding the trailing slash, without generating a 301 loop.
#Options -MultiViews
RewriteEngine On
RewriteCond %{SERVER_PORT} 443
RewriteRule ^(.*)$ http://www.advogadosaqui.com.br/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
## Adding a trailing slash <<<< (HERE IS WHATS I TRIED) >>>>
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301]
# external redirect rule to remove /artigos/ from URLs
RewriteCond %{THE_REQUEST} \s/artigos/(\S*)\sHTTP [NC]
RewriteRule ^ /%1 [L,R=301]
# external redirect rule to remove /unidades/pagina_agencia/ from URLs
RewriteCond %{THE_REQUEST} \s/+unidades/pagina_agencia/(\S*)\sHTTP [NC]
RewriteRule ^ /%1 [L,R=301]
# external redirect rule to remove /unidades/pagina_locker/ from URLs
RewriteCond %{THE_REQUEST} \s/+unidades/pagina_locker/(\S*)\sHTTP [NC]
RewriteRule ^ /%1 [L,R=301]
# external redirect rule to remove /unidades/pagina_estado/ from URLs
RewriteCond %{THE_REQUEST} \s/+unidades/pagina_estado/(\S*)\sHTTP [NC]
RewriteRule ^ /%1 [L,R=301]
# external redirect rule to remove /unidades/pagina_cidade/ from URLs
RewriteCond %{THE_REQUEST} \s/+unidades/pagina_cidade/(\S*)\sHTTP [NC]
RewriteRule ^ /%1 [L,R=301]
# external redirect rule to remove /unidades/pagina_bairro/ from URLs
RewriteCond %{THE_REQUEST} \s/+unidades/pagina_bairro/(\S*)\sHTTP [NC]
RewriteRule ^ /%1 [L,R=301]
# Remove .php extension externally
# To externally redirect /dir/file.php to /dir/file
# %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
#RewriteRule ^ /%1 [R=301,NE,L]
#Hide and Redirect Extension
RewriteCond %{THE_REQUEST} ^[A-Z]+\s.+\.php\sHTTP
RewriteRule ^(.+)\.php$ /$1 [R=301,L]
# internal rewrite from root to /artigos/
RewriteCond %{HTTP_HOST} advogadosaqui [NC]
RewriteCond %{DOCUMENT_ROOT}/artigos/$1.php -f
RewriteRule ^([\w-]+)/?$ artigos/$1.php [L]
# internal rewrite from root to /unidades/pagina_agencia/
RewriteCond %{HTTP_HOST} advogadosaqui [NC]
RewriteCond %{DOCUMENT_ROOT}/unidades/pagina_agencia/$1.php -f
RewriteRule ^([\w-]+)/?$ unidades/pagina_agencia/$1.php [L]
# internal rewrite from root to /unidades/pagina_locker/
RewriteCond %{HTTP_HOST} advogadosaqui [NC]
RewriteCond %{DOCUMENT_ROOT}/unidades/pagina_locker/$1.php -f
RewriteRule ^([\w-]+)/?$ unidades/pagina_locker/$1.php [L]
# internal rewrite from root to /unidades/pagina_estado/
RewriteCond %{HTTP_HOST} advogadosaqui [NC]
RewriteCond %{DOCUMENT_ROOT}/unidades/pagina_estado/$1.php -f
RewriteRule ^([\w-]+)/?$ unidades/pagina_estado/$1.php [L]
# internal rewrite from root to /unidades/pagina_cidade/
RewriteCond %{HTTP_HOST} advogadosaqui [NC]
RewriteCond %{DOCUMENT_ROOT}/unidades/pagina_cidade/$1.php -f
RewriteRule ^([\w-]+)/?$ unidades/pagina_cidade/$1.php [L]
# internal rewrite from root to /unidades/pagina_bairro/
RewriteCond %{HTTP_HOST} advogadosaqui [NC]
RewriteCond %{DOCUMENT_ROOT}/unidades/pagina_bairro/$1.php -f
RewriteRule ^([\w-]+)/?$ unidades/pagina_bairro/$1.php [L]
# handle .php extension internally
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteCond %{DOCUMENT_ROOT}/$1.php -f
#RewriteRule ^(.+?)/?$ $1.php [L]
# remove .php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
##ErrorDocument 404 https://www.advogadosaqui.com.br/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . / [L,R=301]
I tried to add this rule, but it's generating a 301 loop:
## Adding a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301]
UPDATED Question
After using Anubhava's rules everything works fine without any 301 loopings, the problem is when the request comes without the trailing slash... because now it's generating one unnecessary 301 to enforce the trailing slash, I need to find a way to do it with only one redirect...
Some prints to explain what's going on:
###FIRST JUMP - THE REQUESTED URL
###SECOND JUMP
###LAST JUMP
Instead of this, I need to do one redirect directly to the URL with: https + www + trailing slash
FINAL UPDATE
Ok I found a solution... using page rules in Cloudflare / removing the enforce https and www from the local file (.htaccess) and changing the rules to add a trailing slash to:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)([^/])$ /$1$2/ [L,R=301]
The final .htaccess looked like this:
Options -MultiViews
RewriteEngine On
# Adding a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)([^/])$ /$1$2/ [L,R=301]
# external redirect rule to remove /artigos/ from URLs
RewriteCond %{THE_REQUEST} \s/artigos/(\S*)\sHTTP [NC]
RewriteRule ^ /%1 [L,R=301]
# Remove .php extension externally
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1/ [R=301,NE,L]
# internal rewrite from root to /artigos/
RewriteCond %{DOCUMENT_ROOT}/artigos/$1.php -f
RewriteRule ^([\w-]+)/?$ artigos/$1.php [L]
# handle .php extension internally
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
By doing this I managed to avoid the extra redirect ;)
You have a massive .htaccess. I will try to combine and merge few rules to shorten the length and also handle adding slash removal:
Options -MultiViews
RewriteEngine On
## Adding a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ https://www.avantitecnologiati.com.br%{REQUEST_URI}/ [L,R=301,NE]
## add www and turn on https in same rule
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
# external redirect rule to remove /unidades/pagina_agencia/ from URLs
RewriteCond %{THE_REQUEST} \s/+(?:artigos|unidades/pagina_(?:agencia|locker|estado|cidade|bairro))/(\S*)\sHTTP [NC]
RewriteRule ^ /%1 [L,R=301,NE]
# Remove .php extension externally
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1/ [R=301,NE,L]
# internal rewrite from root to /artigos/
RewriteCond %{DOCUMENT_ROOT}/artigos/$1.php -f
RewriteRule ^([\w-]+)/?$ artigos/$1.php [L]
RewriteCond %{DOCUMENT_ROOT}/unidades/pagina_agencia/$1.php -f
RewriteRule ^([\w-]+)/?$ unidades/pagina_agencia/$1.php [L]
RewriteCond %{DOCUMENT_ROOT}/unidades/pagina_locker/$1.php -f
RewriteRule ^([\w-]+)/?$ unidades/pagina_locker/$1.php [L]
RewriteCond %{DOCUMENT_ROOT}/unidades/pagina_estado/$1.php -f
RewriteRule ^([\w-]+)/?$ unidades/pagina_estado/$1.php [L]
RewriteCond %{DOCUMENT_ROOT}/unidades/pagina_cidade/$1.php -f
RewriteRule ^([\w-]+)/?$ unidades/pagina_cidade/$1.php [L]
RewriteCond %{DOCUMENT_ROOT}/unidades/pagina_bairro/$1.php -f
RewriteRule ^([\w-]+)/?$ unidades/pagina_bairro/$1.php [L]
# handle .php extension internally
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
Make sure to use a different browser or remove cache data from your browser to test this rule to avoid old cache.
With your shown samples, attempts please try following .htaccess rules file. Apart from fix of trailing slashes I have clubbed 4 rules into 1 Rule.
Make sure to clear your browser cache before testing your URLs.
#Options -MultiViews
RewriteEngine On
RewriteCond %{SERVER_PORT} 443
RewriteRule ^(.*)/?$ http://www.avantitecnologiati.com.br/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)/?$ http://www.%{HTTP_HOST}/$1 [R=301,L]
## Adding a trailing slash.....
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+[^/])$ $1/ [L,R=301]
# external redirect rule to remove /artigos/ from URLs
RewriteCond %{THE_REQUEST} \s/artigos/(\S*)\sHTTP [NC]
RewriteRule ^ /%1 [L,R=301]
# external redirect rule to remove /unidades/pagina_agencia/ from URLs
RewriteCond %{THE_REQUEST} \s/+unidades/pagina_agencia/(\S*)\sHTTP [NC]
RewriteRule ^ /%1 [L,R=301]
# external redirect rule to remove /unidades/pagina_locker/ from URLs
RewriteCond %{THE_REQUEST} \s/+unidades/pagina_(?:locker|estado|cidade|bairro)/(\S*)\sHTTP [NC]
RewriteRule ^ /%1 [L,R=301]
# Remove .php extension externally
# To externally redirect /dir/file.php to /dir/file
# %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
#RewriteRule ^ /%1 [R=301,NE,L]
#Hide and Redirect Extension
RewriteCond %{THE_REQUEST} ^[A-Z]+\s.+\.php\sHTTP
RewriteRule ^(.+)\.php$ /$1 [R=301,L]
# internal rewrite from root to /artigos/
RewriteCond %{HTTP_HOST} avantitecnologiati [NC]
RewriteCond %{DOCUMENT_ROOT}/artigos/$1.php -f
RewriteRule ^([\w-]+)/?$ artigos/$1.php [L]
# internal rewrite from root to /unidades/pagina_agencia/
RewriteCond %{HTTP_HOST} avantitecnologiati [NC]
RewriteCond %{DOCUMENT_ROOT}/unidades/pagina_agencia/$1.php -f
RewriteRule ^([\w-]+)/?$ unidades/pagina_agencia/$1.php [L]
# internal rewrite from root to /unidades/pagina_locker/
RewriteCond %{HTTP_HOST} avantitecnologiati [NC]
RewriteCond %{DOCUMENT_ROOT}/unidades/pagina_locker/$1.php -f
RewriteRule ^([\w-]+)/?$ unidades/pagina_locker/$1.php [L]
# internal rewrite from root to /unidades/pagina_estado/
RewriteCond %{HTTP_HOST} avantitecnologiati [NC]
RewriteCond %{DOCUMENT_ROOT}/unidades/pagina_estado/$1.php -f
RewriteRule ^([\w-]+)/?$ unidades/pagina_estado/$1.php [L]
# internal rewrite from root to /unidades/pagina_cidade/
RewriteCond %{HTTP_HOST} avantitecnologiati [NC]
RewriteCond %{DOCUMENT_ROOT}/unidades/pagina_cidade/$1.php -f
RewriteRule ^([\w-]+)/?$ unidades/pagina_cidade/$1.php [L]
# internal rewrite from root to /unidades/pagina_bairro/
RewriteCond %{HTTP_HOST} avantitecnologiati [NC]
RewriteCond %{DOCUMENT_ROOT}/unidades/pagina_bairro/$1.php -f
RewriteRule ^([\w-]+)/?$ unidades/pagina_bairro/$1.php [L]
# handle .php extension internally
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteCond %{DOCUMENT_ROOT}/$1.php -f
#RewriteRule ^(.+?)/?$ $1.php [L]
# remove .php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
##ErrorDocument 404 http://www.avantitecnologiati.com.br/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . / [L,R=301]

Multiple redirects with 301 htaccess

I am new to redirects and regex. I have an issue with redirects implemented. Here is the example. I want
http://www.example.com/support redirect to
https://www.example.com/support/
But here is what happens
http://www.example.com/support redirects to
https://www.example.com/support redirects to
https://www.example.com/support/
Below is the htaccess contents for it
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
# To remove double slash in the middle
RedirectMatch 301 ^/(.*)//(.*)$ https://www.example.com/$1/$2/
# redirect index.php to root
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)/index\.php$ https://www.example.com/$1 [R=301]
#index to root
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ https://www.example.com/$1 [R=301,L]
You can use these rules t avoid multiple redirecects:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L,NE]
## Adding a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301,NE]
# To remove double slash anywhere
RewriteCond %{THE_REQUEST} \s[^?]*//
RewriteRule ^.*$ /$0 [R=302,L,NE]
# redirect index.php to parent
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteCond %{REQUEST_URI} ^(.*/)index\.php$ [NC]
RewriteRule ^ %1 [L,R=301,NE]
Make sure to use a new browser for testing.
#anubhava your edit shows the attachment 1
The existing code in the question I posted does below
However, I am looking for a single redirect to the final destination..
Recent update by adding trailing slash results are below

.htaccess remove index.php remove www. force https

I understand this is not necessarily an EE specific question, but it involves the classic EE removal of index.php from the URL as well as a couple more specific rewrites. It’s also a lazy question, as I’ve tried a few rewrite rule combinations and there are errors… so, what I’m after is (please).
Remove www.
Force https://
Remove index.php
The important note, is that on an (mt) Media Temple server when testing on subdomains such as dev.domain.com the good old multiple redirects problem is kicking in. Ideally there’s a way for one .htaccess file to rule them all. Many thanks in advance.
# Enable Rewrite Engine
RewriteEngine On
RewriteBase /
# Force https
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Remove the www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
#Remove index.php
#strip index.php from the URL if that is all that is given
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\ HTTP/
RewriteRule ^(([^/]+/)*)index\.php$ http://%{HTTP_HOST}/ [R=301,NS,L,QSA]
#strip index.php/* from the URL
RewriteCond %{THE_REQUEST} ^[^/]*/index\.php/ [NC]
RewriteRule ^index\.php/(.+) http://%{HTTP_HOST}/$1 [R=301,L,QSA]
# EE
#rewrite all non-image/js/css urls back to index.php if they are not files or directories
RewriteCond $1 !^(images|templates|themes)/ [NC]
RewriteCond $1 !\.(css|js|gif|jpe?g|png) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L,QSA]
What worked for me (excluding the index.php part, only for removing WWW and forcing HTTPS), was this:
# Enable Rewrite Engine
RewriteEngine On
# Remove the www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
# Force https, specify http_host
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^example\.com$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#-----the following part is untested, but should work to answer the full quesiton including the index.php part
#Remove index.php
#strip index.php from the URL if that is all that is given
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\ HTTP/
RewriteRule ^(([^/]+/)*)index\.php$ http://%{HTTP_HOST}/ [R=301,NS,L,QSA]
#strip index.php/* from the URL
RewriteCond %{THE_REQUEST} ^[^/]*/index\.php/ [NC]
RewriteRule ^index\.php/(.+) http://%{HTTP_HOST}/$1 [R=301,L,QSA]
# EE
#rewrite all non-image/js/css urls back to index.php if they are not files or directories
RewriteCond $1 !^(images|templates|themes)/ [NC]
RewriteCond $1 !\.(css|js|gif|jpe?g|png) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L,QSA]
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/js/.*$
RewriteRule ^(.*)$ /public/$1 [QSA,L]
RewriteCond %{REQUEST_URI} ^/css/.*$
RewriteRule ^(.*)$ /public/$1 [QSA,L]
RewriteCond %{REQUEST_URI} ^/images/.*$
RewriteRule ^(.*)$ /public/$1 [QSA,L]
RewriteCond %{REQUEST_URI} ^/fonts/.*$
RewriteRule ^(.*)$ /public/$1 [QSA,L]
RewriteCond %{REQUEST_URI} ^/themes/.*$
RewriteRule ^(.*)$ /public/$1 [QSA,L]
RewriteCond %{REQUEST_URI} ^/inc/.*$
RewriteRule ^(.*)$ /public/$1 [QSA,L]
RewriteCond %{REQUEST_URI} ^/admin.php$
RewriteRule ^(.*)$ /public/$1 [QSA,L]
RewriteCond %{REQUEST_URI} !^/public/.*$
RewriteRule ^(.*)$ /public/index.php?$1 [QSA,L]
</IfModule>

.htaccess WildCard SSL for sub-domain

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]

https in htaccess and order of rules (using Expression Engine)

I'm building a site in expression engine that part of needs to be https. The site is also using a new domain name (new one www.example-new.com the old one www.example.old.com).
I want to do the following things:
remove the index.php
force www
force https for any url starting www.example.old.com/extranet
redirect https URLs that are not www.example.old.com/extranet (e.g. www.example.old.com/news to http
I have the following code so far that works for the first two requirements:
<IfModule mod_rewrite.c>
RewriteEngine On
# Force www
RewriteCond %{HTTP_HOST} ^example-new.com$ [NC]
RewriteRule ^(.*)$ http://www.example-new.com/$1 [R=301,L]
# Removes index.php
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
AddType x-httpd-php53 .php
I seem to be going round in circles, so I've two questions that will help me write the other rewrites (although feel free to share suggestions...):
1) Should the code for requirements 3 and 4 be positioned before "removes index.php" code?
2) Does the position have any bearing on the redirects that will be coming from the old site e.g. www.example-old.com/some-link-here.asp will be redirected to www.example-new.com/some-new-link-here
Thanks,
Gregor
1) Remove 'index.php' from ExpressionEngine URLs
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
2) Add 'www' to all URIs
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
3) Force https:// for any URI starting with /extranet
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/extranet(.*)$ [NC]
RewriteRule . https://%{HTTP_HOST}%{REQUEST_URI}$1 [R=301,L]
4) Redirect https:// URIs that are not /extranet
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/extranet(.*)$ [NC]
RewriteRule . http://%{HTTP_HOST}%{REQUEST_URI}$1 [R=301,L]
Putting it all together, here's your complete set of RewriteRules:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/extranet(.*)$ [NC]
RewriteRule . https://%{HTTP_HOST}%{REQUEST_URI}$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/extranet(.*)$ [NC]
RewriteRule . http://%{HTTP_HOST}%{REQUEST_URI}$1 [R=301,L]
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
1) Should the code for requirements 3 and 4 be positioned before "removes index.php" code?
The rules are processed in the order that you write them. I expect that you want the site to redirect first then do the remove index, so yes, it should go before
2) Does the position have any bearing on the redirects that will be coming from the old site e.g. www.example-old.com/some-link-here.asp will be redirected to www.example-new.com/some-new-link-here
If you are using the same directory for both sites, then you would need to prefix all rules for one site with a RewriteCond that limits the domain. Otherwise, order of the rules (old vs new site) is important.
Your redirects from the old site should also incorporate rules in the new e.g. ensure that you are going to http/s as necessary to avoid extra redirects.
Below is the code to force http/s
#if not secure
RewriteCond %{HTTPS} off
#and starts with /extranet
RewriteCond %{REQUEST_URI} ^/extranet [NC]
#redirect to secure
RewriteRule . https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#if secure
RewriteCond %{HTTPS} on
#and does not start with /extranet
RewriteCond %{REQUEST_URI} !^/extranet [NC]
#redirect to http
RewriteRule . http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# force www on hostname, but keep same protocol (http/https)
RewriteCond %{HTTP_HOST} !^www\.(.+)
RewriteCond %{HTTPS}s ^(on(s)|offs)
RewriteRule ^ http%2://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
If it helps... I just did this yesterday for an ecommerce site... Here's my .htaccess file
# Remove WWW from URL
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
# Add a trailing slash to paths without an extension
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteCond %{REQUEST_METHOD} !=POST
RewriteRule ^(.*)$ $1/ [L,R=301]
#remove index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1
#Force HTTPS on checkout/account pages
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} (checkout|account)
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
#remove HTTPS on all other pages
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(img|_|images|checkout|account)
RewriteRule ^(.*)$ http://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

Resources