Adding additional Apache2 rewrites without infinite redirect

Viewed 25

I currently have the following in a .htaccess file to rewrite the .php extention:

# /var/www/html/help/.htaccess

# To externally redirect /dir/foo.php to /dir/foo excluding POST requests
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=302,L,NE]

# To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]

Which works as expected. However I'm looking to add some additional rewrites, such as redirecting /help/open to /help/submit. Everything I've tried seems to end with infinite redirects from /help/open -> /help/submit and back.

RewriteRule open.php /help/submit [R=302,NE,L]
RewriteRule submit /help/open.php [L]

Can someone advise how I could implement additional URL rewrites here without resulting in infinite loops?

1 Answers

You are getting infinite loop error because your rules redirect the destination file help/open.php to itself . You can use an END flag in your second rule to fix this problem.

RewriteRule open.php /help/submit [R=302,NE,L]
RewriteRule submit /help/open.php [END]

Note : The END flag only works on Apache version 2.4 or higher. If you are using an older version of Apache then use the following solution instead

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule open.php /help/submit [R=302,NE,L]
RewriteRule submit /help/open.php [L]
Related