I have written a .htaccess for the subdomain but it redirects too many times.
my complete code looks like
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ http://subdomain/$1 [R,L]
RewriteEngine On
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^404/?$ /404.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ http://subdomain/404 [L,R]
2
Answers
The issue most likely is the topmost redirection. It implements a redirection loop. Also it is unclear what it is actually meant to achieve … What is "subdomain" here? A hostname / subdomain name? So something like
http://sub.example.com/$1
?Assuming that all "sub domains" you served by the same http server you’d need to add an additional condition to break that look:
That could also be simplified to this:
Also consider using the encrypted https protocol, it actually is the standard these days.
You’ve not actually stated what you are trying to do. However, the only reason to check the
SERVER_PORT
for port80
like this is if you are redirecting to HTTPS (port 443). But you are redirecting to HTTP (port 80) – so this will naturally result in a redirect loop.You need to change
http
tohttps
in the substitution string. For example:(Ultimately, this should be a 301 permanent redirect, once you have confirmed it works as intended.)
OR, remove these directives entirely if you are not intending to force HTTPS (but you should be).
And change to
https
in the last rule. (However, you shouldn’t be redirecting to a "404" error document to begin with.)