如果重定向文件存在则重写 htaccess

发布于 2024-10-26 20:42:41 字数 1087 浏览 2 评论 0原文

问题:某些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

澉约 2024-11-02 20:42:41

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

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

第一个条件检查文件名是否以 .html 结尾,第二个条件使用第一个条件中的反向引用 %1 来检查 .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. :)

情归归情 2024-11-02 20:42:41

我还想将所有 .html 请求重写为 .php 文件,但前提是 .php 文件存在。但我的变化是,只有当实际的 .html 文件不存在时才会发生这种情况。

因此,只有对不存在的 .html 文件的调用才会被重写为 .php 文件(如果它们确实存在):(我还在 XAMP 本地服务器和 Apache 在线服务器上成功测试了这一点!)

# Rewrite rules to .html file to .php if the .php version
# does actually exist.

RewriteCond %{REQUEST_FILENAME} (.*)\.html [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteCond %1\.php -f [NC]
RewriteRule ^(.*).html$ $1.php [NC]
  1. 它首先检查如果请求的文件是.html文件(我们不希望.jpg、.css、.pdf等被重写)。
  2. 它检查该 .html 文件是否不存在。 (如果 .php 确实存在,我们不想重写它。)
  3. 然后它检查 .php 版本是否确实存在(我们不想重写到不存在的 .php 文件)。
  4. 然后它将 .html 重写为 .php

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

# Rewrite rules to .html file to .php if the .php version
# does actually exist.

RewriteCond %{REQUEST_FILENAME} (.*)\.html [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteCond %1\.php -f [NC]
RewriteRule ^(.*).html$ $1.php [NC]
  1. It first check if the requested file is a .html file (we don't want .jpg, .css, .pdf, etc. to be rewritten).
  2. The it checks if that .html file does not exist. (we don't want to rewrite to .php if it actually does exist.)
  3. Then it checks if a .php version does exist (we don't want to rewrite to a non existing .php file).
  4. Then it rewrites the .html to .php
叹倦 2024-11-02 20:42:41

很抱歉这么晚才回答,但您需要添加 RewriteBase 指令才能使其正常工作。

我有同样的问题(与你的http:/stufff)并以这种方式修复:

RewriteBase   /your/application_path
RewriteCond %{REQUEST_FILENAME} (.*)\.html$
RewriteCond %1\.php -f
RewriteRule ^(.*)\.html$ $1.php [L]

希望它会有所帮助!

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 :

RewriteBase   /your/application_path
RewriteCond %{REQUEST_FILENAME} (.*)\.html$
RewriteCond %1\.php -f
RewriteRule ^(.*)\.html$ $1.php [L]

Hope it will help !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文