argon
argon

Reputation: 449

htaccess redirect in subdomain causes address issue

Prelude

As shown in the picture below, there is a problem in the .htaccess code that follows (after the picture).

screenshot


The code

RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ .anon.php [L]

The Purpose

What I'm trying to do here is to have only an .htaccess file and a .anon.php file that can be used either in document root -or inside a sub-folder where the sub-domain resides; however, all requests must be directed at the .anon.php file.

The Issue

The above redirect rules work very well, but for some reason when a folder exists -then it prepends the current subdirectory (sub-domain's folder) to the request.

The code above does not cause that redirect-loop as seen; the loop comes from some PHP code that I wrote to try and mitigate this issue, because: when you manually change the address in the address bar (after it redirected wrong the first time) -then all subsequent requests/refreshes work fine - 100%

I had to clear all browsing data/cache in order to re-produce this issue. The redirect-loop as explained does not happen in chromium-based browsers such as Brave Chrome Opera .. strange but true.

The Solution

The only issue is that it prepends the existing sub-domain's folder to the address in the redirect -only if the requested folder exists. The solution would be to instruct the rules to ... "not do that" -but I don't know how, been at this for 2 days now and I know the solution should be fairly simple.

I've found many "sub-domain-as-root" examples, but none of them fixes this exact issue. Any answers and comments would be appreciated -and rewarded, thank you.

Upvotes: 0

Views: 34

Answers (1)

Luís Garcés
Luís Garcés

Reputation: 65

this maybe can help you

 RewriteEngine on 
RewriteCond %{HTTP_HOST} ^(www.)?PUTYOURDOMAINNAMEHERE.com$ 
RewriteCond %{REQUEST_URI} !^/PUTYOURFOLDERHERE/ 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ /PUTYOURFOLDERHERE/$1  
RewriteCond %{HTTP_HOST} ^(www.)?PUTYOURDOMAINNAMEHERE.com$ 
RewriteRule ^(/)?$ PUTYOURFOLDERHERE/ [L]
Options +SymLinksIfOwnerMatch

Upvotes: 1

Related Questions