title: Redirecting all URLs containing a word to a URL using htaccess
body: <p>I'm trying the create a htaccess rule to redirect urls that contain a certain word except for two pages.</p>
  
  <p>Example:</p>
  
  <pre><code>https://www.example.com/string
  </code></pre>
  
  <p>to:</p>
  
  <pre><code>https://www.example.com
  </code></pre>
  
  <hr>
  
  <pre><code>https://www.example.com/string/some-page.html
  </code></pre>
  
  <p>to:</p>
  
  <pre><code>https://www.example.com/some-page.html
  </code></pre>
  
  <hr>
  
  <pre><code>https://www.example.com/string/some-directory/some-page.html
  </code></pre>
  
  <p>to:</p>
  
  <pre><code>https://www.example.com/some-directory/some-page.html
  </code></pre>
  
  <hr>
  
  <pre><code>https://www.example.com/string/some-directory/
  </code></pre>
  
  <p>to:</p>
  
  <pre><code>https://www.example.com/some-directory/
  </code></pre>
  
  <hr>
  
  <p>EXCEPT NOT REDIRECT THESE TWO PAGES:</p>
  
  <pre><code>https://www.example.com/string/checkout/cart/
  </code></pre>
  
  <p>to:</p>
  
  <pre><code>https://www.example.com/string/checkout/cart/
  </code></pre>
  
  <hr>
  
  <p>and</p>
  
  <pre><code>https://www.example.com/string/checkout/onepage/#/steps
  </code></pre>
  
  <p>to:</p>
  
  <pre><code>https://www.example.com/string/checkout/onepage/#/steps
  </code></pre>
  
  <p>I've already tried all the solutions I could find on Google.I can't seem to even get the basics to work!</p>
  
  <p>Any help would be much appreciated. Thanks </p>
  
  <p><strong>EDITED TO ADD CURRENT REWRITE RULES BELOW</strong></p>
  
  <pre><code>    ModPagespeed on
      AllowOverride None
      AllowOverrideList None
      Require all granted
      DirectoryIndex index.php
      ErrorDocument 401 default
      ErrorDocument 401 "Authorization Required"
      ErrorDocument 403 "Authorization Required"
      ErrorDocument 500 /error-500.html
      Options All -Indexes +FollowSymlinks
      Options All
  
      SSLOptions
  
      RewriteEngine On
      RewriteBase /
  
      RewriteRule ^api/rest api.php?type=rest [QSA,L]     
      RewriteRule ^(.*)local.xml$ - [F,L] 
      RewriteCond %{THE_REQUEST} ^.*/index.php
      RewriteRule ^(.*)index.php$ https://www.example.com$1 [R=301,L]
  
      RewriteCond %{HTTP_HOST} ^example.com
      RewriteRule (.*) https://www.example.com$1 [R=301,L]    
      RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
      RewriteCond %{REQUEST_URI} !^/(media|skin|js)/
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{REQUEST_FILENAME} !-l
      RewriteRule .* index.php [L]
      RewriteCond %{HTTP_HOST} ^11\.11\.11\.111
      RewriteRule (.*) https://www.example.com$1 [R=301,L]
  
  #RewriteCond %{HTTP:Accept-Language} (es) [NC]
  #RewriteRule .* https://www.example.com/mx [L]
  
      ##### Block unwanted Crawler Bots that clog your server #####
  
      RewriteCond %{HTTP_USER_AGENT} libwww-perl.* 
      RewriteRule .* ? [F,L]
      RewriteCond %{HTTP_USER_AGENT} MJ12bot
      RewriteRule .* - [F]
      RewriteCond %{HTTP_USER_AGENT} 80legs [NC]
      RewriteRule ^ - [F]
  
      ##############################################################
      ## Remove Bad Bots from crawling ##
      # IF THE UA STARTS WITH THESE
      # Block spambots
      #
      RewriteCond %{HTTP_USER_AGENT} ^(aesop_com_spiderman|alexibot|backweb|bandit|batchftp|bigfoot) [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} ^(black.?hole|blackwidow|blowfish|botalot|buddy|builtbottough|bullseye) [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} ^(cheesebot|cherrypicker|chinaclaw|collector|copier|copyrightcheck) [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} ^(cosmos|crescent|curl|custo|da|diibot|disco|dittospyder|dragonfly) [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} ^(drip|easydl|ebingbong|ecatch|eirgrabber|emailcollector|emailsiphon) [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} ^(emailwolf|erocrawler|exabot|eyenetie|filehound|flashget|flunky) [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} ^(frontpage|getright|getweb|go.?zilla|go-ahead-got-it|gotit|grabnet) [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} ^(grafula|harvest|hloader|hmview|httplib|httrack|humanlinks|ilsebot) [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} ^(infonavirobot|infotekies|intelliseek|interget|iria|jennybot|jetcar) [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} ^(joc|justview|jyxobot|kenjin|keyword|larbin|leechftp|lexibot|lftp|libweb) [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} ^(likse|linkscan|linkwalker|lnspiderguy|lwp|magnet|mag-net|markwatch) [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} ^(mata.?hari|memo|microsoft.?url|midown.?tool|miixpc|mirror|missigua) [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} ^(mister.?pix|moget|mozilla.?newt|nameprotect|navroad|backdoorbot|nearsite) [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} ^(net.?vampire|netants|netcraft|netmechanic|netspider|nextgensearchbot) [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} ^(attach|nicerspro|nimblecrawler|npbot|octopus|offline.?explorer) [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} ^(offline.?navigator|openfind|outfoxbot|pagegrabber|papa|pavuk) [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} ^(pcbrowser|php.?version.?tracker|pockey|propowerbot|prowebwalker) [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} ^(psbot|pump|queryn|recorder|realdownload|reaper|reget|true_robot) [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} ^(repomonkey|rma|internetseer|sitesnagger|siphon|slysearch|smartdownload) [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} ^(snake|snapbot|snoopy|sogou|spacebison|spankbot|spanner|sqworm|superbot) [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} ^(superhttp|surfbot|asterias|suzuran|szukacz|takeout|teleport) [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} ^(telesoft|the.?intraformant|thenomad|tighttwatbot|titan|urldispatcher) [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} ^(turingos|turnitinbot|urly.?warning|vacuum|vci|voideye|whacker) [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} ^(libwww-perl|widow|wisenutbot|wwwoffle|xaldon|xenu|zeus|zyborg|anonymouse) [NC,OR]
      #
      ## STARTS WITH WEB
      #
      RewriteCond %{HTTP_USER_AGENT} ^web(zip|emaile|enhancer|fetch|go.?is|auto|bandit|clip|copier|master|reaper|sauger|site.?quester|whack) [NC,OR]
      #
      ## ANYWHERE IN UA -- GREEDY REGEX
      #
      RewriteCond %{HTTP_USER_AGENT} ^.*(craftbot|download|extract|stripper|sucker|ninja|clshttp|webspider|leacher|collector|grabber|webpictures).*$ [NC]
      #
      RewriteRule . - [F,L]
      #
      ## Useragents starting with
      #
      RewriteCond %{HTTP_USER_AGENT} ^atraxbot [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} ^Azureus [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} ^geohasher [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} ^PycURL [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} ^Python-urllib [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} ^research-scan-bot [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} ^Sosospider [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} ^Wget [NC,OR]
      RewriteCond %{HTTP_USER_AGENT} ^xenu [NC,OR]
      #
      ## User agents contains string
      #
      RewriteCond %{HTTP_USER_AGENT} ^.*casper [NC,OR]
      RewriteCond %{REQUEST_METHOD} ^(HEAD|TRACE|DELETE|TRACK) [NC,OR]    
      #
      ## Block out use of illegal or unsafe characters in the HTTP Request
      #
      RewriteCond %{THE_REQUEST} ^.*(\\r|\\n|%0A|%0D).* [NC,OR]
      #
      ## Block out use of illegal or unsafe characters in the Referer Variable of the HTTP Request
      ## RewriteCond %{HTTP_REFERER} ^(.*)(&lt;|&gt;|'|'|%0A|%0D|%27|%3C|%3E|%00).* [NC,OR]
      ## Block out use of illegal or unsafe characters in any cookie associated with the HTTP Request
      #
      RewriteCond %{HTTP_COOKIE} ^.*(&lt;|&gt;|'|'|%0A|%0D|%27|%3C|%3E|%00).* [NC,OR]
      #
      ## Block out use of illegal characters in URI or use of malformed URI
      #
      RewriteCond %{REQUEST_URI} ^/(,|;|:|&lt;|&gt;|"&gt;|"&lt;|/|\\\.\.\\).{0,9999}.* [NC,OR]
      #
      ## Block out  use of empty User Agent Strings
      ## NOTE - disable this rule if your site is integrated with Payment Gateways such as PayPal
      ## RewriteCond %{HTTP_USER_AGENT} ^$ [OR]
      ## Block out  use of illegal or unsafe characters in the User Agent variable
      #
      RewriteCond %{HTTP_USER_AGENT} ^.*(&lt;|&gt;|'|'|%0A|%0D|%27|%3C|%3E|%00).* [NC,OR]
      #
      ## Measures to block out  SQL injection attacks
      #
      RewriteCond %{QUERY_STRING} ^.*(;|&lt;|&gt;|'|"|"|'|\)|%0A|%0D|%22|%27|%3C|%3E|%00).*(/\*|union|select|insert|cast|set|declare|drop|update|md5|benchmark).* [NC,OR]
      #
      ## Block out  reference to localhost/loopback/127.0.0.1 in the Query String
      #
      RewriteCond %{QUERY_STRING} ^.*(localhost|loopback|127\.0\.0\.1).* [NC,OR]
      #
      ## Block out  use of illegal or unsafe characters in the Query String variable
      #
      RewriteCond %{QUERY_STRING} ^.*(&lt;|&gt;|'|'|%0A|%0D|%27|%3C|%3E|%00).* [NC]
      #
      ########## Begin - File injection protection, by SigSiu.net
      RewriteCond %{REQUEST_METHOD} GET
      RewriteCond %{QUERY_STRING} [a-zA-Z0-9_]=http:// [OR]
      RewriteCond %{QUERY_STRING} [a-zA-Z0-9_]=(\.\.//?)+ [OR]
      RewriteCond %{QUERY_STRING} [a-zA-Z0-9_]=/([a-z0-9_.]//?)+ [NC]
      RewriteRule .* - [F]
      ########## End - File injection protection  
  </code></pre>