如果重定向文件存在的htaccess重写 [英] htaccess rewrite if redirected file exists

查看:264
本文介绍了如果重定向文件存在的htaccess重写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:一些HTML页面的PHP当量(apple.html,apple.php; orange.html,orange.php),但不是所有做(grapes.html)

The problem: Some html pages of php equivalents (apple.html, apple.php; orange.html, orange.php), but not all do (grapes.html).

我们的目标:如果PHP的版本存在,改写,否则保持它的HTML版本

The goal: If the php version exists, rewrite, otherwise keep it the html version.

我有:

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)\.html$ /$1.php [R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)\.php$ /$1.html [R]

有趣的问题:

Interesting issues:

如果我不把/在$ 1.PHP然后我结束了面前:site.com/document/root/path(即:site.com/home/user/www/file.php)

If I don't put / in front of $1.php then I end up with: site.com/document/root/path (ie: site.com/home/user/www/file.php)

在拨打第二重写规则,我得到 http://site.com/http:/site.com /page.html ,它告诉我有太多的重定向。请注意,只有一个/第二HTTP。

When calling the second RewriteRule, I get http://site.com/http:/site.com/page.html and it tells me there were too many redirects. Notice how there is only one / in the second http.

我已经取得了一些进展,我加的RewriteBase /和之前的$ 1中卸下的/​​,但我仍然得到了过多的重定向错误(site.com/page.html网页生成了过多的重定向。清除Cookie对本网站或允许第三方Cookie可能会解决这个问题。如果不是,则可能是服务器配置的问题,而不是你的电脑出了问题)。

I've made some progress, I added RewriteBase / and removed the / before the $1, but I still get the too many redirects error (the web page at site.com/page.html has resulted in too many redirects. Clearing your cookies for this site or allowing third-party cookies may fix the problem. If not, it is possibly a server configuration issue and not a problem with your computer).

好像如果我只是改写HTML - > PHP - > HTML我得到同样的错误。所以它看起来像逻辑工作,但不允许那种逻辑。我想不出我可以看到,如果一个PH​​P版本文件的存在,任何其他的办法吗?我能想到的唯一的办法是做类似的事情来:

It seems like if I just rewrite html -> php -> html I get the same error. So it looks like the logic is working, but that sort of logic isn't allowed. I can't think of any other way I could see if a "php version" of a file exists? The only way I can think of is do something similar to:

RewriteCond ^(.*)\.html$ $1.php -f
RewriteRule ^(.*)\.html$ $1.php [R]

不幸的是这完全不是那么回事(我猜是因为它有三个部分的条件线)。我想获得的文件名不带扩展名,​​然后说,如果filename.php是一个文件,重写page.html即可到page.php

Unfortunately that doesn't quite work (I'm guessing because it has three segments on the condition line). I'm trying to get the filename without the extension, then say if filename.php is a file, rewrite page.html to page.php

推荐答案

您应该能够实现,通过使用两个条件:

you should be able to achieve that by using two conditions:

RewriteCond %{REQUEST_FILENAME} (.*)\.html$
RewriteCond %1\.php -f
RewriteRule ^(.*)\.html$ $1.php [R,L]

如果文件名结束了。html的第二个使用背面参考%1的第一个条件,以检查是否.PHP版本存在的第一个条件检查。

The first condition checks if the filename ended with .html and the second uses the back reference %1 from the first condition to check if .php version exists.

希望它帮助。 :)

这篇关于如果重定向文件存在的htaccess重写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆