简体   繁体   中英

Only .php files loop with .htaccess redirect

I'm setting up a system with wildcard subdomains directed to subfolders. For reasons beyond me, this has to be done with .htaccess. For instance, to redirect http://subdomain.example.com to the directory /subdomain/ under example.com's public_html, the following partially works (with thanks to this Q&A ):

RewriteEngine On
RewriteRule ^subdomain/ - [L]
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$
RewriteRule ^(.*)$ /subdomain/$1 [L]

It's working for all types of files except if they have extension .php. For example, if I drop a sometext.txt file in the /subdomain/ directory, I can get it at http://subdomain.example.com/sometext.txt but if I change the text file's extension to .php, I get a 500 error because of a redirect loop.

That makes me think that there's something strange going on with how the PHP module tries to read the file. Or something in the relationship between Apache and PHP. I'm not familiar with this; that's why I'm asking.

I'm on a shared host so I can't access the Apache configuration.

Can you try if doing it like this works for you?

RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain\.localhost$
RewriteRule ^(.*)$ http://localhost/subdomain/$1 [L]

so instead of localhost, try adding directly your actual domain url.

I tested this on my localhost, with such urls first, and then they redirect correctly

http://subdomain.localhost/test.txt
http://subdomain.localhost/test.php

I'll suggest relax a bit, take coffee and comeback. (sometimes this helps)

Then focus on just the redirection,

  • rule out issue of their Apache config
  • you can worry bout 301 later
  • work with test.php only, no index.php

try this on your debugging:

  • close all other chrome browser tabs.
  • open in incognito, the url you want to test
  • keep closing that incognito so its always for sure a full refresh
  • change htacess file, and always open a new incognito tab

As I suspected, the problem was caused by the hosting provider's configuration. The .htaccess is fine. I'm told this is a recently discovered issue in DirectAdmin with php-fpm and FastCgi. (The solution was for them to remove a symlink to public_html at my home directory and replace it with an empty directory. I don't know any more about this!)

Thanks to all!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM