Setup Permanent 301 Redirects after moving to Https [closed]

i moved my site from http to https
now i want to redirect the user into the new url but evry time i’m trying to edit .htaccess file i’m getting an error

this is the working file content where xxxz is site name

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress
RewriteCond %{HTTP_HOST} ^xxxz\.qa$ [OR]
RewriteCond %{HTTP_HOST} ^www\.xxxz\.qa$
RewriteCond %{REQUEST_URI} !^/\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]    {32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^/?$ "http\:\/\/xxxz\.com\.qa" [R=301,L]

what i’m trying to edit is to add the following code in the top of the file as i read

RewriteEngine on
RewriteCond %{HTTP_HOST} ^xxxz.com.qa [NC,OR]
RewriteCond %{HTTP_HOST} ^www.xxxz.com.qa [NC]
RewriteRule ^(.*)$ https://www.xxxz.com.qa/$1 [L,R=301,NC]

but its not working so what do u think

2 Answers
2

RewriteCond %{HTTP_HOST} ^xxxz.com.qa [NC,OR]
RewriteCond %{HTTP_HOST} ^www.xxxz.com.qa [NC]
RewriteRule ^(.*)$ https://www.xxxz.com.qa/$1 [L,R=301,NC]

This will result in a redirect loop as it will repeatedly redirect https://www.xxxz.com.qa/<url> to https://www.xxxz.com.qa/<url> again and again…

If you want to redirect from HTTP then you need to check that the request was for HTTP before redirecting to HTTPS. For example:

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]

No need for the NC flag, as you are simply matching everything anyway.


However, this conflicts with the existing directives at the end of your file:

RewriteCond %{HTTP_HOST} ^xxxz\.qa$ [OR]
RewriteCond %{HTTP_HOST} ^www\.xxxz\.qa$
RewriteCond %{REQUEST_URI} !^/\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]    {32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^/?$ "http\:\/\/xxxz\.com\.qa" [R=301,L]

These directives should probably be deleted. (Fortunately, they probably aren’t doing anything anyway – because they are after the WordPress front-controller.)

Leave a Comment