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:
The htaccess file can be used to password protect directories, control access to files and folders. With this file you can set up URL redirects and URL rewriting,. Also you can block unwanted visitors or bots, and more. It uses regular expressions and tranzitive words to specify conditions and actions.
The file can also be used to optimize website performance by enabling caching, compression, and other techniques. Overall, the htaccess file is a powerful tool for web developers and site administrators to customize and control server behavior.
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í
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.
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.