Redirecting domain without www to domain with www using .htaccess

Last updated December 6th, 2023 06:18

The following code in the .htaccess file allows you to redirecting domain without www to domain with www using .htaccess:

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

				
			

This is a set of Apache mod_rewrite directives written in the .htaccess file to redirect any requests that are made to a non-www domain to the www version of the same domain.

Let’s go through each line of the code:

				
					RewriteEngine On
				
			

This line enables the mod_rewrite module, which allows the server to perform URL rewriting.

				
					RewriteCond %{HTTP_HOST} !^www\. [NC]
				
			

This line sets a condition that the following RewriteRule will only be applied if the HTTP_HOST variable does not start with “www.” (the “^” symbol means “starts with”). The “NC” flag makes the comparison case-insensitive.

				
					RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

				
			

This line specifies the actual rewriting rule. It captures the requested path using the regular expression “^(.*)$” (which means “match any character zero or more times”), and appends it to the “www.” version of the HTTP_HOST variable. The [R=301,L] flags indicate that the redirection should be permanent (301) and that this is the last rule that should be applied (L).

Redirecting domain without www to domain with www using .htaccess

In summary, this set of directives redirects any requests made to a non-www domain to the corresponding www domain, with a permanent 301 redirect.

The website is created with care for the included information. I strive to provide high-quality and useful content that helps or inspires others. If you are satisfied with my work and would like to support me, you can do so through simple options.

Byl pro Vás tento článek užitečný?

Klikni na počet hvězd pro hlasování.

Průměrné hodnocení. 0 / 5. Počet hlasování: 0

Zatím nehodnoceno! Buďte první

Jak užitečný vidíte tento článek.

Sledujte mě na sociálních médiích.

Je mi líto, že pro Vás nebyl článek užitečný.

Jak mohu vylepšit článek?

Řekněte mi, jak jej mohu zlepšit.

newsletter

Subscribe to the Newsletter

Stay informed! Join our newsletter subscription and be the first to receive the latest information directly to your email inbox. Follow updates, exclusive events, and inspiring content, all delivered straight to your email.

Odebírat
Upozornit na
guest
0 Komentáře/ů
Nejstarší
Nejnovější Nejvíce hlasováno
Vložené zpětné vazby.
Zobrazit všechny komentáře.

Pokud mi chcete napsat rychlou zprávu, využije, prosím, níže uvedený
kontaktní formulář. Děkuji.

Další Kontaktní údaje