Ankit
Ankit

Reputation: 64

Too Many Redirects Error. When trying to redirect a url

My .htaccess file has this code:-

RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

During Seo, my site shows 2 URL duplicate error as:- https://example.net/contact-us.php & https://example.net/contact-us points to the same resource. So, we need to redirect the URL. For that, I wrote this code:-

Redirect 301 /contact-us.php https://example.net/contact-us/

The problem is It shows too many redirects Error. Kindly Help. Thanks in advance.

Upvotes: 0

Views: 98

Answers (1)

Panama Jack
Panama Jack

Reputation: 24478

You should have 2 rules. 1 to redirect no extension to php file and 1 to redirect php to no extension if someone types it in. You should not need thatRedirect 301 rule.

I would have my rules like below.

Replace these rules

RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

with this

RewriteEngine On

#redirect a direct request for the php file to no extension
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\s/([^\s]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)/?$ /$1.php [L]

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

Upvotes: 1

Related Questions