Apoorv Saxena
Apoorv Saxena

Reputation: 4226

Redirect loop occuring while removing/adding trailing Slash from URL

While writing a rewrite rule specifically for only a particular URL to remove a trailing slash from it, an infinite redirect loop is occuring.

The following is the code I am trying to code in .htaccess:

RewriteRule ^abc.php /abc/ [R=301,L,NC,QSA]
RewriteRule ^abc/ abc.php [NC,QSA]

I am trying to make a 301 Redirect on abc.php to abc/ but want to serve abc/ with abc.php's content only. Want to write a Rule specifically for this url only.

Upvotes: 1

Views: 518

Answers (2)

Vicent
Vicent

Reputation: 5452

You can break the infinite loop if you check the HTTP request line (see here and also here) sent to the server by the browser with a RewriteCond and, in addition, you reverse the order of the rules:

RewriteRule ^abc/ abc.php [L,QSA]
RewriteCond %{THE_REQUEST} ^GET\ /abc\.php
RewriteRule ^abc.php$ /abc/ [R=301, QSA]

Upvotes: 2

shawndreck
shawndreck

Reputation: 2069

Try this :

RewriteEngine On

RewriteRule ^abc/? abc.php [NC,L,QSA]
RewriteRule ^abc.php /abc/ [R=301,QSA]

Hope it helps!

Upvotes: 0

Related Questions