snipt

Ctrl+h for KB shortcuts

ApacheConf

HTACCESS - File Name and File Type Conditions

1
2
3
4
5
6
# Don't redirect these files
RewriteCond %{REQUEST_URI} !(page1|page_one|page_one_1)\.html$
# Don't redirect these file types
RewriteCond %{REQUEST_FILENAME} !\.(css|js|xml|jpg|png|gif|eot|woff|ttf)$
# Redirect everything else
RewriteRule ^(.*)$ http://%{HTTP_HOST}/newpage.html [R=301,L]
https://snipt.net/embed/16c83996f079b1553da1c6d3f7324472/
/raw/16c83996f079b1553da1c6d3f7324472/
16c83996f079b1553da1c6d3f7324472
apacheconf
ApacheConf
6
2019-06-23T01:55:18
True
False
False
/api/public/snipt/115787/
htaccess-filename-and-file-type-conditions
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="c"># Don&#39;t redirect these files</span> </span><span id="L-2"><a name="L-2"></a><span class="nb">RewriteCond</span> %{REQUEST_URI} !(page1|page_one|page_one_1)\.html$ </span><span id="L-3"><a name="L-3"></a><span class="c"># Don&#39;t redirect these file types</span> </span><span id="L-4"><a name="L-4"></a><span class="nb">RewriteCond</span> %{REQUEST_FILENAME} !\.(css|js|xml|jpg|png|gif|eot|woff|ttf)$ </span><span id="L-5"><a name="L-5"></a><span class="c"># Redirect everything else</span> </span><span id="L-6"><a name="L-6"></a><span class="nb">RewriteRule</span> ^(.*)$ http://%{HTTP_HOST}/newpage.html [R=301,L] </span></pre></div> </td></tr></table>
htaccess

PHP

Check current post in the loop

1
2
3
4
5
6
<?php 
// Insert code after the second post
if( 1 == $wp_query->current_post ):
	echo '<div>some text</div>';
endif;
?>
https://snipt.net/embed/3312e090a761fb7270a9510de72dfedb/
/raw/3312e090a761fb7270a9510de72dfedb/
3312e090a761fb7270a9510de72dfedb
php
PHP
6
2019-06-23T01:55:38
True
False
False
Feb 13, 2014 at 02:02 PM
/api/public/snipt/113494/
check-current-post-in-the-loop
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="cp">&lt;?php</span> </span><span id="L-2"><a name="L-2"></a><span class="c1">// Insert code after the second post</span> </span><span id="L-3"><a name="L-3"></a><span class="k">if</span><span class="p">(</span> <span class="mi">1</span> <span class="o">==</span> <span class="nv">$wp_query</span><span class="o">-&gt;</span><span class="na">current_post</span> <span class="p">)</span><span class="o">:</span> </span><span id="L-4"><a name="L-4"></a> <span class="k">echo</span> <span class="s1">&#39;&lt;div&gt;some text&lt;/div&gt;&#39;</span><span class="p">;</span> </span><span id="L-5"><a name="L-5"></a><span class="k">endif</span><span class="p">;</span> </span><span id="L-6"><a name="L-6"></a><span class="cp">?&gt;</span><span class="x"></span> </span></pre></div> </td></tr></table>
php, wordpress

PHP

WordPress - Redirect all users, except admins

1
2
3
4
5
6
7
8
<?php 
if (current_user_can( 'manage_options' )) {
     // enjoy
} else {
     header('Location: http://domain.com/coming-soon.html');
	 exit();
}
?>
https://snipt.net/embed/ce142f6986961f4bdef4cd32bf802a12/
/raw/ce142f6986961f4bdef4cd32bf802a12/
ce142f6986961f4bdef4cd32bf802a12
php
PHP
8
2019-06-24T07:03:10
True
False
False
/api/public/snipt/95136/
wordpress-redirect-all-but-users-except-admins
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a> <a href="#L-7">7</a> <a href="#L-8">8</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="cp">&lt;?php</span> </span><span id="L-2"><a name="L-2"></a><span class="k">if</span> <span class="p">(</span><span class="nx">current_user_can</span><span class="p">(</span> <span class="s1">&#39;manage_options&#39;</span> <span class="p">))</span> <span class="p">{</span> </span><span id="L-3"><a name="L-3"></a> <span class="c1">// enjoy</span> </span><span id="L-4"><a name="L-4"></a><span class="p">}</span> <span class="k">else</span> <span class="p">{</span> </span><span id="L-5"><a name="L-5"></a> <span class="nb">header</span><span class="p">(</span><span class="s1">&#39;Location: http://domain.com/coming-soon.html&#39;</span><span class="p">);</span> </span><span id="L-6"><a name="L-6"></a> <span class="k">exit</span><span class="p">();</span> </span><span id="L-7"><a name="L-7"></a><span class="p">}</span> </span><span id="L-8"><a name="L-8"></a><span class="cp">?&gt;</span><span class="x"></span> </span></pre></div> </td></tr></table>
php, wordpress

ApacheConf

HTACCESS - Redirect Old/Missing Files to their New Versions

1
2
3
4
5
6
7
# This rule redirects any HTML files that are missing
# to the PHP version of that file
##########################################################

RewriteCond %{REQUEST_URI} \.html$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)\.html /$1\.php [R=301,L]
https://snipt.net/embed/47cb1e8a2d6ec826afb3a100b050f340/
/raw/47cb1e8a2d6ec826afb3a100b050f340/
47cb1e8a2d6ec826afb3a100b050f340
apacheconf
ApacheConf
7
2019-06-23T01:55:48
True
False
False
/api/public/snipt/65783/
htaccess-redirect-oldmissing-files-to-their-new-versions
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a> <a href="#L-7">7</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="c"># This rule redirects any HTML files that are missing</span> </span><span id="L-2"><a name="L-2"></a><span class="c"># to the PHP version of that file</span> </span><span id="L-3"><a name="L-3"></a><span class="c">##########################################################</span> </span><span id="L-4"><a name="L-4"></a> </span><span id="L-5"><a name="L-5"></a><span class="nb">RewriteCond</span> %{REQUEST_URI} \.html$ </span><span id="L-6"><a name="L-6"></a><span class="nb">RewriteCond</span> %{REQUEST_FILENAME} !-f </span><span id="L-7"><a name="L-7"></a><span class="nb">RewriteRule</span> ^(.*)\.html /$1\.php [R=301,L] </span></pre></div> </td></tr></table>
htaccess

Text only

Extract Anchor Text from URL

1
2
3
4
5
http://www.domain.com/directory/service.html#service_2
In the URL above, this regex will only pull 'service_2'.
============================================================

\b(?<=#)[a-z][a-z0-9_]*\b
https://snipt.net/embed/132ddc493cd60f24650c281d567cd19b/
/raw/132ddc493cd60f24650c281d567cd19b/
132ddc493cd60f24650c281d567cd19b
text
Text only
5
2019-06-23T01:55:53
True
False
False
/api/public/snipt/53776/
extract-anchor-text-from-url
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a>http://www.domain.com/directory/service.html#service_2 </span><span id="L-2"><a name="L-2"></a>In the URL above, this regex will only pull &#39;service_2&#39;. </span><span id="L-3"><a name="L-3"></a>============================================================ </span><span id="L-4"><a name="L-4"></a> </span><span id="L-5"><a name="L-5"></a>\b(?&lt;=#)[a-z][a-z0-9_]*\b </span></pre></div> </td></tr></table>
regex