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

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

问题描述

问题:php 等价物的一些 html 页面(apple.html、apple.php;orange.html、orange.php),但并非所有页面都可以(grapes.html).

目标:如果php版本存在,则重写,否则保留html版本.

我有:

选项 +FollowSymLinks重写引擎开启重写规则 ^(.*).html$/$1.php [R]RewriteCond %{REQUEST_FILENAME} !-f重写规则 ^(.*).php$/$1.html [R]

有趣的问题:

如果我不把/放在 $1.php 前面,那么我最终会得到:site.com/document/root/path(即:site.com/home/user/www/file.php)

在调用第二个 RewriteRule 时,我得到 http://site.com/http:/site.com/page.html 它告诉我重定向太多了.注意第二个http中只有一个/.

我取得了一些进展,我添加了 RewriteBase/并删除了 $1 之前的/,但我仍然收到了太多重定向错误(site.com/page.html 上的网页导致了太多重定向.清除此站点的 cookie 或允许第三方 cookie 可能会解决问题.如果没有,则可能是服务器配置问题,而不是您的计算机问题).

似乎如果我只是重写 html -> php -> html 我会得到同样的错误.所以看起来逻辑是有效的,但这种逻辑是不允许的.我想不出任何其他方式可以查看文件的php 版本"是否存在?我能想到的唯一方法是做类似的事情:

RewriteCond ^(.*).html$ $1.php -f重写规则 ^(.*).html$ $1.php [R]

不幸的是,这并不完全有效(我猜是因为它在条件行上有三个部分).我正在尝试获取没有扩展名的文件名,然后说如果 filename.php 是一个文件,请将 page.html 重写为 page.php

解决方案

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

RewriteCond %{REQUEST_FILENAME} (.*).html$重写条件 %1.php -f重写规则 ^(.*).html$ $1.php [R,L]

第一个条件检查文件名是否以 .html 结尾,第二个条件使用第一个条件中的反向引用 %1 来检查 .php 版本是否存在.

希望有帮助.:)

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

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

I have:

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

Interesting issues:

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)

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.

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).

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]

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]

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.

Hope it helps. :)

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

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