mod_rewrite从地址栏中删除.html,而无需更改html文件中的链接 [英] mod_rewrite to remove .html from address bar without changing links in the html file

查看:76
本文介绍了mod_rewrite从地址栏中删除.html,而无需更改html文件中的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一个问题。因此,我在Apache上有一个简单的网站 website.com:

First question. So I have a simple website "website.com" on Apache:

website.com
    index.html
    site1.html
    site2.html

在每个html中,我都有其他链接,

Within each html I have links to other, in this form:

<a href="index.html">main</a>
<a href="site1.html">site1</a>
<a href="site2.html">site2</a>

我希望浏览器显示 website.com/site1而不是 website.com/site1 .html。

I want the browser to display "website.com/site1" instead of "website.com/site1.html".

我已经玩了几天,最后总是得到以下结果之一:

I've been playing with it for a few days and I always end up with one of these results:


  1. 无限次重定向。

  2. 内部服务器错误。

  3. 两个版本都可以工作(例如 website.com/site1.html和 website.com/site1),但.html保留在地址栏中。

我不知道发生了什么。我什至无法正确打破它。例如,我希望此.htaccess会在访问 website.com/site1时引发错误,因为没有规则将site1重定向到site1.html:

I don't know what's going on. I can't even break it properly. For example I expected that this .htaccess will throw an error on accessing "website.com/site1", because there is no rule redirecting site1 to site1.html:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)\.html$ /$1 [R=302,QSA]

但是没有-有效,只是不会删除地址栏中的.html。

But no - it works, just doesn't delete .html in the address bar.

Apache如何知道将 website.com/site1重定向到 website.com/site1.html没有合适的RewriteRule吗?我的理解是错误的,为了使它正常工作,我必须从website.com/site1重定向到website.com/site1.html?

How Apache knows to redirect "website.com/site1" to "website.com/site1.html" without the proper RewriteRule for it? Is my understanding wrong, that I have to redirect from website.com/site1 to website.com/site1.html to make it work?

谢谢。

推荐答案

您的规则的最后一行:

RewriteRule ^(.*)\.html$ /$1 [R=302,QSA]

这意味着仅将任何 file.html 更改为 file ,而您需要的是使 file 仅将其内部重定向到 file.html ,例如:

It means to change any file.html to file only and what you need is to accpet file only the redirect it internally to file.html like this :

RewriteRule ^(.*)$ /$1.html [L,QSA]

但是,在这种情况下,如果要删除 .html file.html 将会保持原样。 c $ c>完全尝试以下规则:

But , in this case , the file.html will be as is so, if you want to remove .html at all try the folowing rules :

RewriteEngine on 
RewriteCond %{THE_REQUEST} \s/+(.*)\.html[\s?/] [NC]
RewriteRule ^  /%1 [R=301,L,NE]

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

后面的前两行 RewriteEngine on 将删除 .html

其余的行将检查请求扩展范围是否为 .html 文件,如果它符合 .html 的要求,

The rest of lines will check whthere the request withut extention is .html file or not , if it is accpeted .html it will be redirected but internally .

现在,都 site1.html site1

Now , both , site1.html or site1 will be site1 in browser.

通过此操作,您将确保文件accepet .html 扩展,然后重定向,这样可以分开与错误文件混合的阶段。

By this , you will make sure that file accepet .html extention before redirecting and that will seperate this stage of being mixed with error files.

这篇关于mod_rewrite从地址栏中删除.html,而无需更改html文件中的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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