I need to have a header for even and odd pages like this:

I have done this so far:
% !TEX TS-program = xelatex
% !TEX encoding = UTF-8
\documentclass{biditufte-book}
\usepackage{ptext}
\usepackage{xcolor}
\usepackage{showframe}
\usepackage{fancyhdr} % Required for header and footer configuration
\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{\markboth{\normalsize\bfseries #1}{}} % Chapter text font settings
\renewcommand{\sectionmark}[1]{\markright{\normalsize\bfseries #1}{}} % Section text font settings
\renewcommand{\headrulewidth}{0pt}
\setlength{\headheight}{16.5pt}% fancyhdr tells you the length
\newlength{\bluerulelength}
\setlength{\bluerulelength}{\textwidth}
\addtolength{\bluerulelength}{\marginparsep}
\addtolength{\bluerulelength}{\marginparwidth}
\newlength{\pagenumberboxskip}
\setlength{\pagenumberboxskip}{-\bluerulelength}
\addtolength{\pagenumberboxskip}{1cm}
\addtolength{\pagenumberboxskip}{10pt}
\fancyhf{}
\fancyhead[LE]{%
\small
\makebox[-5pt][r]{ \pagenumbox{1cm}}%
\pagehead{\quad\quad\quad\quad\nouppercase{\leftmark}\hfill}%
}
\fancyhead[LO]{%
\small
\makebox[\pagenumberboxskip][r]{ \pagenumbox{1cm}}%
\pagehead{\hfill\nouppercase{\rightmark}\quad\quad\quad\quad}%
}
\newcommand{\pagenumbox}[1]{%
\begingroup\fboxsep=0pt
\colorbox{blue!20}{\pagestrut
\makebox[#1][c]{\thepage}%
}%
\endgroup
}
\newcommand{\pagehead}[1]{%
\begingroup\fboxsep=0pt
\colorbox{blue}{\pagestrut
\makebox[\bluerulelength][s]{#1}%
}%
\endgroup
}
\newcommand{\pagestrut}{%
\vrule width 0pt
height 1.5\ht\strutbox
depth 1.5\dp\strutbox
}
% Removes the header from odd empty pages at the end of chapters
\makeatletter
\renewcommand{\cleardoublepage}{
\clearpage\ifodd\c@page\else
\hbox{}
\vspace*{\fill}
\thispagestyle{empty}
\newpage
\fi}
\usepackage[localise=on]{xepersian}
\settextfont{Times New Roman}
\setdigitfont{Times New Roman}
\begin{document}
\chapter{مشاهدهپذیری}
\ptext
\section{مقدمه}
\ptext[1-10]
\section{مقدمه}
\ptext[1]
\end{document}
which creates something like this:
But that's not what I want. Can someone help me?

\fancyhfoffset[L]{some length}to shift the header to the left. Andsome lengthwould probably be the sum of\hoffsetand\oddsidemargin(or\evensidemargin). Howeverbidifiddles withfancyhdrand that no longer works. Now you have to useRinstead ofL. I would suggest you try something like\fancyhoffset[RE}{some value}and\fancyhfoffset[RO]{other value}, and just with trial and error (and measuring) find out what these values should be. And you have to put that after the\begin{document}. – Pieter van Oostrum Dec 20 '16 at 18:39