{"id":3807,"date":"2022-09-08T19:37:40","date_gmt":"2022-09-08T19:37:40","guid":{"rendered":"https:\/\/scriptfeeds.com\/blog\/?p=3807"},"modified":"2023-03-27T13:30:51","modified_gmt":"2023-03-27T13:30:51","slug":"add-canonical-tag-via-htaccess-file","status":"publish","type":"post","link":"https:\/\/scriptfeeds.com\/blog\/marketing\/add-canonical-tag-via-htaccess-file\/","title":{"rendered":"2 Easy Ways to Add Canonical Tag via htaccess file in website"},"content":{"rendered":"\n<p>Adding canonical URLs for your website is an important part of good\u00a0SEO. In this post, we\u2019ll update you about several\u00a0adding canonical tag via htaccess file and different ways\u00a0for removing and adding the\u00a0<code>www<\/code>\u00a0prefix, apart from that we will let you know how to remove any\u00a0<code>index.php<\/code>\u00a0appendage from all\u00a0URIs.<\/p>\n\n\n\n<p>To add these code snippets, you need .<a href=\"https:\/\/docs.oracle.com\/cd\/B14099_19\/web.1012\/q20206\/howto\/htaccess.html\" target=\"_blank\" rel=\"noopener\">htaccess <\/a>file\u00a0in your website root directory. Please make sure to take backup of .htaccess before adding any code.<\/p>\n\n\n\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\"><h2>Table of Contents<\/h2><nav><ul><li><a href=\"#how-to-remove-www-from-all-ur-ls-canonical-tag-via-htaccess-file\">How to remove www from all URLs : Canonical Tag via htaccess file<\/a><ul><\/ul><\/li><li><a href=\"#require-www-for-all-ur-ls-canonical-tag-via-htaccess-file\">Require www for all URLs : Canonical Tag via htaccess file<\/a><ul><li><a href=\"#this-code-of-htaccess-adds-the-www-prefix-to-all-ur-ls\">This code of .htaccess adds the www prefix to all URLs:<\/a><\/li><li><a href=\"#remove-the-index-php-from-all-ur-ls-canonical-tag-via-htaccess-file\">Remove the index.php from all URLs : Canonical Tag via htaccess file<\/a><ul><\/ul><\/li><li><a href=\"#how-to-remove-www-and-index-php-canonical-tag-via-htaccess-file\">How to remove www and index.php : Canonical Tag via htaccess file<\/a><ul><\/ul><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n\n\n\n<p>TIP : Best way take backup is either download .htaccess file or renaming the existing one with .bak and the creating a new .htaccess file.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" width=\"705\" height=\"354\" src=\"https:\/\/scriptfeeds.com\/blog\/wp-content\/uploads\/2023\/03\/Add-Canonical-Tag-via-htaccess-file.jpeg\" alt=\"Add Canonical Tag via htaccess file\" class=\"wp-image-4057\" title=\"Add Canonical Tag via htaccess file\" srcset=\"https:\/\/scriptfeeds.com\/blog\/wp-content\/uploads\/2023\/03\/Add-Canonical-Tag-via-htaccess-file.jpeg 705w, https:\/\/scriptfeeds.com\/blog\/wp-content\/uploads\/2023\/03\/Add-Canonical-Tag-via-htaccess-file-300x151.jpeg 300w, https:\/\/scriptfeeds.com\/blog\/wp-content\/uploads\/2023\/03\/Add-Canonical-Tag-via-htaccess-file-150x75.jpeg 150w, https:\/\/scriptfeeds.com\/blog\/wp-content\/uploads\/2023\/03\/Add-Canonical-Tag-via-htaccess-file-399x200.jpeg 399w, https:\/\/scriptfeeds.com\/blog\/wp-content\/uploads\/2023\/03\/Add-Canonical-Tag-via-htaccess-file-360x180.jpeg 360w\" sizes=\"(max-width: 705px) 100vw, 705px\" \/><figcaption class=\"wp-element-caption\">Code of .htaccess file in WordPress Installation<\/figcaption><\/figure>\n<\/div>\n\n\n<h2 class=\"has-medium-font-size wp-block-heading\" id=\"how-to-remove-www-from-all-ur-ls-canonical-tag-via-htaccess-file\"><strong>How to remove www from all URLs<\/strong> : <strong>Canonical Tag via htaccess file<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"this-code-of-htaccess-removes-the-www-prefix-from-all-ur-ls\" style=\"font-size:16px\"><strong>This code of .htaccess removes the www prefix from all&nbsp;URLs:<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;IfModule mod_rewrite.c&gt;\n\t\n\tRewriteCond %{HTTP_HOST} ^www\\.example\\.com$ &#91;NC]\n\tRewriteRule (.*) https:\/\/example.com\/$1 &#91;R=301,L]\n\t\n&lt;\/IfModule&gt;<\/code><\/pre>\n\n\n\n<p class=\"has-vivid-red-color has-text-color\">Replace&nbsp;<code>example.com<\/code>&nbsp;with your domain name.<\/p>\n\n\n\n<h2 class=\"has-medium-font-size wp-block-heading\" id=\"require-www-for-all-ur-ls-canonical-tag-via-htaccess-file\"><strong>Require www for all URLs<\/strong> : <strong>Canonical Tag via htaccess file<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"this-code-of-htaccess-adds-the-www-prefix-to-all-ur-ls\" style=\"font-size:16px\"><strong>This code of .htaccess adds the www prefix to all URLs:<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;IfModule mod_rewrite.c&gt;\n\t\n\tRewriteCond %{HTTP_HOST} ^example\\.com$ &#91;NC]\n\tRewriteRule (.*) https:\/\/www.example.com\/$1 &#91;R=301,L]\n\t\n&lt;\/IfModule&gt;<\/code><\/pre>\n\n\n\n<p class=\"has-vivid-red-color has-text-color\">Replace&nbsp;<code>example.com<\/code>&nbsp;with your domain name.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"remove-the-index-php-from-all-ur-ls-canonical-tag-via-htaccess-file\"><strong>Remove the index.php from all URLs<\/strong> : <strong>Canonical Tag via htaccess file<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"this-code-of-htaccess-strips-the-index-php-to-all-ur-ls\" style=\"font-size:16px\"><strong>This code of .htaccess strips the&nbsp;<code>index.php<\/code> to all URLs:<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;IfModule mod_rewrite.c&gt;\n\t\n\tRewriteCond %{THE_REQUEST} ^&#91;A-Z]{3,9}\\ \/(&#91;^\/]+\/)*index\\.php\\ HTTP\/\n\tRewriteRule ^((&#91;^\/]+\/)*)index\\.php$ https:\/\/example.com\/$1 &#91;R=301,L]\n\t\n&lt;\/IfModule&gt;<\/code><\/pre>\n\n\n\n<p class=\"has-vivid-red-color has-text-color\">Replace&nbsp;<code>example.com<\/code>&nbsp;with your domain name.<\/p>\n\n\n\n<h3 class=\"has-medium-font-size wp-block-heading\" id=\"how-to-remove-www-and-index-php-canonical-tag-via-htaccess-file\"><strong>How to remove www and index.php<\/strong> : <strong>Canonical Tag via htaccess file<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"here-is-an-example-of-how-to-remove-www-and-index-php\" style=\"font-size:16px\"><strong>Here is an example of how to remove www and index.php:<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;IfModule mod_rewrite.c&gt;\n\t\n\tRewriteCond %{HTTP_HOST} ^www\\.example\\.com$ &#91;NC]\n\tRewriteRule (.*) https:\/\/example.com\/$1 &#91;R=301,L]\n\t\n\tRewriteCond %{THE_REQUEST} ^&#91;A-Z]{3,9}\\ \/(&#91;^\/]+\/)*index\\.php\\ HTTP\/\n\tRewriteRule ^((&#91;^\/]+\/)*)index\\.php$ https:\/\/example.com\/$1 &#91;R=301,L]\n\t\n&lt;\/IfModule&gt;<\/code><\/pre>\n\n\n\n<h5 class=\"has-vivid-red-color has-text-color wp-block-heading\" id=\"notes\"><strong>Notes<\/strong><\/h5>\n\n\n\n<p>If you get a 500 server error for any of the above techniques, try replacing&nbsp;<code>[R=301,L]<\/code>&nbsp;with&nbsp;<code>[R,L]<\/code>, it will work.<\/p>\n\n\n\n<p>So that&#8217;s all folks, let us know how it works in comment section. Connect with our <a href=\"https:\/\/scriptfeeds.com\/blog\/hire-us\/\" data-type=\"page\" data-id=\"3537\">developer here<\/a> in case something goes wrong.<\/p>\n\n\n\n<p>Happy Coding!<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Adding canonical URLs for your website is an important part of good\u00a0SEO. In this post, we\u2019ll update you about several\u00a0adding canonical tag via htaccess file and different ways\u00a0for removing and adding the\u00a0www\u00a0prefix, apart from that we will let you know how to remove any\u00a0index.php\u00a0appendage from all\u00a0URIs. To add these code snippets, you need .htaccess file\u00a0in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3814,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jnews-multi-image_gallery":[],"jnews_single_post":{"subtitle":"","format":"standard","video":"","gallery":"","source_name":"","source_url":"","via_name":"","via_url":"","override_template":"0","override":[{"template":"2","single_blog_custom":"","parallax":"1","fullscreen":"1","layout":"right-sidebar-narrow","sidebar":"default-sidebar","second_sidebar":"default-sidebar","sticky_sidebar":"1","share_position":"float","share_float_style":"share-monocrhome","show_share_counter":"1","show_view_counter":"1","show_featured":"1","show_post_meta":"1","show_post_author":"1","show_post_author_image":"1","show_post_date":"1","post_date_format":"default","post_date_format_custom":"Y\/m\/d","show_post_category":"1","show_post_reading_time":"0","post_reading_time_wpm":"300","show_zoom_button":"0","zoom_button_out_step":"2","zoom_button_in_step":"3","show_post_tag":"1","show_prev_next_post":"0","show_popup_post":"0","number_popup_post":"1","show_author_box":"0","show_post_related":"1","show_inline_post_related":"0"}],"override_image_size":"0","image_override":[{"single_post_thumbnail_size":"no-crop","single_post_gallery_size":"crop-500"}],"trending_post":"0","trending_post_position":"meta","trending_post_label":"Trending","sponsored_post":"0","sponsored_post_label":"Sponsored by","sponsored_post_name":"","sponsored_post_url":"","sponsored_post_logo_enable":"0","sponsored_post_logo":"","sponsored_post_desc":"","disable_ad":"0"},"jnews_primary_category":{"id":"","hide":""},"footnotes":""},"categories":[99,242],"tags":[260,257,258],"class_list":["post-3807","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-marketing","category-seo","tag-add-canonical-tag-for-seo-via-htaccess-file","tag-add-canonical-tag-with-htaccess","tag-best-way-to-add-canonical-tag"],"_links":{"self":[{"href":"https:\/\/scriptfeeds.com\/blog\/wp-json\/wp\/v2\/posts\/3807","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/scriptfeeds.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/scriptfeeds.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/scriptfeeds.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/scriptfeeds.com\/blog\/wp-json\/wp\/v2\/comments?post=3807"}],"version-history":[{"count":0,"href":"https:\/\/scriptfeeds.com\/blog\/wp-json\/wp\/v2\/posts\/3807\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/scriptfeeds.com\/blog\/wp-json\/wp\/v2\/media\/3814"}],"wp:attachment":[{"href":"https:\/\/scriptfeeds.com\/blog\/wp-json\/wp\/v2\/media?parent=3807"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/scriptfeeds.com\/blog\/wp-json\/wp\/v2\/categories?post=3807"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/scriptfeeds.com\/blog\/wp-json\/wp\/v2\/tags?post=3807"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}