简体   繁体   English

只有.php文件以.htaccess重定向循环

[英]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. 出于我以外的原因,必须使用.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 ): 例如,要将http://subdomain.example.com重定向到example.com的public_html下的目录/ subdomain /,以下部分工作(感谢此问答 ):

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. 它适用于所有类型的文件, 除非文件扩展名为.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. 例如,如果我将sometext.txt文件放在/ subdomain /目录中,则可以从http://subdomain.example.com/sometext.txt获得它,但是如果我将文本文件的扩展名更改为.php,我会得到由于重定向循环而导致500错误。

That makes me think that there's something strange going on with how the PHP module tries to read the file. 这让我认为PHP模块尝试读取文件的方式有些奇怪。 Or something in the relationship between Apache and PHP. 还是Apache和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. 我在共享主机上,因此无法访问Apache配置。

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. 因此,请尝试直接添加您的实际域网址,而不是localhost。

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 排除他们的Apache配置问题
  • you can worry bout 301 later 你可以稍后再担心301
  • work with test.php only, no index.php 仅适用于test.php,无index.php

try this on your debugging: 在调试时尝试以下操作:

  • close all other chrome browser tabs. 关闭所有其他Chrome浏览器标签。
  • 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 更改htacess文件,并始终打开一个新的隐身标签

As I suspected, the problem was caused by the hosting provider's configuration. 正如我所怀疑的那样,问题是由托管服务提供商的配置引起的。 The .htaccess is fine. .htaccess很好。 I'm told this is a recently discovered issue in DirectAdmin with php-fpm and FastCgi. 有人告诉我这是DirectAdmin中使用php-fpm和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!) (解决方案是让他们在我的主目录中删除指向public_html的符号链接,并将其替换为空目录。对此我一无所知!)

Thanks to all! 谢谢大家!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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