如果重定向文件存在则重写 htaccess
问题:某些 html 页面具有 php 等效项(apple.html、apple.php;orange.html、orange.php),但并非全部都是(grapes.html)。
目标:如果php版本存在,则重写,否则保留html版本。
我有:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)\.html$ /$1.php [R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)\.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如果没有,则可能是服务器配置问题,而不是您的计算机问题)。
看起来如果我只是重写 html -> php-> html 我得到同样的错误。所以看起来这个逻辑是有效的,但这种逻辑是不允许的。我想不出任何其他方法可以查看文件的“php 版本”是否存在?我能想到的唯一方法是做类似的事情:
RewriteCond ^(.*)\.html$ $1.php -f
RewriteRule ^(.*)\.html$ $1.php [R]
不幸的是,这不太有效(我猜测是因为它在条件线上有三个段)。我试图获取不带扩展名的文件名,然后说如果 filename.php 是一个文件,则将 page.html 重写为 page.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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该能够通过使用两个条件来实现这一点:
第一个条件检查文件名是否以 .html 结尾,第二个条件使用第一个条件中的反向引用 %1 来检查 .php 版本是否存在。
希望有帮助。 :)
you should be able to achieve that by using two conditions:
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. :)
我还想将所有 .html 请求重写为 .php 文件,但前提是 .php 文件存在。但我的变化是,只有当实际的 .html 文件不存在时才会发生这种情况。
因此,只有对不存在的 .html 文件的调用才会被重写为 .php 文件(如果它们确实存在):(我还在 XAMP 本地服务器和 Apache 在线服务器上成功测试了这一点!)
I also wanted to Rewrite all .html request to .php files, but only if the .php file exist. But my variation was that this should only happen if the actual .html file does not exist.
So only calls to .html files that does not exist is Rewritten to .php file, if they do exists by these rules: (I also have tested this on a XAMP local server and also a Apache online server with success!)
很抱歉这么晚才回答,但您需要添加 RewriteBase 指令才能使其正常工作。
我有同样的问题(与你的http:/stufff)并以这种方式修复:
希望它会有所帮助!
I'm sorry to answer sooooo late but you will need to add the RewriteBase directive to make it works.
I had the same problem (with your http:/stufff) and fixed it this way :
Hope it will help !