Home > Back-end >  How should I write these rewrite rules in my .htaccess file
How should I write these rewrite rules in my .htaccess file

Time:05-21

I have this in my .htaccess file:

RewriteEngine on

RewriteCond %{HTTP_HOST} ^admin\.domain1\.com$ [OR]
RewriteCond %{HTTP_HOST} ^admin\.domain2\.com$ [OR]
RewriteCond %{HTTP_HOST} ^admin\.domain3\.com$
RewriteRule (.*) /path/to/directory/$1 [L]

RewriteCond %{HTTP_HOST} ^admin\.domain4\.com$
RewriteRule (.*) /different/path/$1 [L]

The first block works, but the second block gives me the following error:

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at [no address given] to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log.

Apache/2.4.29 (Ubuntu) Server at admin.domain4.com.ar Port 80

What am I doing wrong??

CodePudding user response:

The first block works, but the second block gives me an Internal Server error:

Both rules/blocks have the same "issue". The first rule will only "work" if you have another .htaccess file in the /path/to/directory/ that also uses mod_rewrite (perhaps to route the URL?) in order to prevent an internal rewrite loop (the probable cause of your Internal Server Error).

The easiest way to resolve this, if you are on Apache 2.4, is it simply use the END flag, instead of L. For example:

RewriteRule (.*) /different/path/$1 [END]

The L flag only stops the current round of processing, it does not stop all processing by the rewrite engine. On the second pass through the rewrite engine, the pattern (.*) also matches the rewritten request /different/path/<url> which gets further rewritten to /different/path/different/path/<url> etc. resulting in an endless loop, unless there is something to stop it.

  • Related