HTACCESS useful Rewrite Codes 2017


(Santhosh veer) #1

HTACCESS useful Rewrite Codes 2017 - For Apache Web servers

For Force HTTP

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} ^https$
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI}
</IfModule>

WWW to NON-WWW

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

NON-WWW TO WWW

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ http://www.example.com/$1 [R,L]

Disable directory listing

Options -Indexes

Custom 404 Error Page

ErrorDocument 404 /404.html

For HTTPS Redirection

SEO Friendly URL

NON-SEO Friendly URL - http://example.com/post.php?id=45
SEO Friendly URL - http://example.com/post-45.html

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^article-([0-9]+)\.html$ post.php?id=$1

Redirecting .html to non .html pages

Redirect subdomain from www to non-www

#Force non-www:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.sub.example.com [NC]
RewriteRule ^(.*)$ http://sub.example.com/$1 [L,R=301]

301 Post Redirection

RewriteEngine On
Redirect 301 /blog-old-post/ http://www.example.com/my-blog-updated-post/

SEO Friendly Profile URL

http://example.com/profile.php?user=santhosh to http://example.com/santhosh

RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?user=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?user=$1

Bookmark This post I will Update New working Codes soon :slight_smile:

Thanks…


How to fix redirecting and Indexing both www and non-www versions?
Simple PHP URL Shortener Plugin :zap:
(Pradeep Kumar) #2

Awesome share bro, all these codes are very helpful during our blogging journey… permalink structure change, https SSL upgrade, etc.


(Santhosh veer) #3

yes bro, I will update More Working Codes soon :blush: bro


Useful Code Snippets and Functions For your WordPress Site :tada:
(Nithin.Sai) #4

I can’t find File Upload in Yoast SEO-Tools.(Advanced is enabled)
Please Refer:
https://app.box.com/s/sind5f700abxeiailrek74ilnvymil6y


(Santhosh veer) #5

Goto tools - File Editor :pencil2:


(Nithin.Sai) #6

I can see Bulk Editor & Import & Export…Nothing else.


(Pradeep Kumar) #7

You’ll have the “.htaccess” and “robots.txt” file editor section bro…