简体   繁体   English

RewriteEngine不会影响URL重写

[英]RewriteEngine doesn't affect URL rewriting

I am trying to make simple URL rewriting (example: '/toto.html' will be displayed as '/toto' in the URL bar). 我正在尝试进行简单的URL重写(例如:“ / toto.html”将在URL栏中显示为“ / toto”)。 I have the following code in my .htaccess file, located at the root of my server (/): 我的.htaccess文件中有以下代码,位于服务器(/)的根目录中:

ErrorDocument 403 /
ErrorDocument 404 /
Options All +FollowSymLinks -MultiViews -indexes
# Apache Rewrite Rules (-html extensions)
RewriteEngine On 
RewriteBase /
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.html\ HTTP
RewriteRule (.*)\.html$ $1 [L,R=301]

It does work: '/toto.html' is rewrited as '/toto'. 它确实起作用:'/toto.html'被重写为'/ toto'。 However, the server keeps trying to redirect me to the page without the .html extension rather than simply rewriting it. 但是,服务器一直在尝试将我重定向到不带.html扩展名的页面,而不是简单地重写它。 I have read a lot of other documented issues about RewriteEngine being ignored and so on, and tried many things, without any success. 我已经阅读了很多其他有关RewriteEngine被忽略等问题的文献记录,并且尝试了很多事情,但都没有成功。 The server is still trying to interpret the URL rather than just rewriting it. 服务器仍在尝试解释URL,而不仅仅是重写URL。

You need one redirect rule and one rewrite rule: 您需要一个重定向规则和一个重写规则:

ErrorDocument 403 /
ErrorDocument 404 /

Options All +FollowSymLinks -MultiViews -indexes
# Apache Rewrite Rules (-html extensions)
RewriteEngine On 
RewriteBase /

# To externally redirect /dir/file.html to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.html [NC]
RewriteRule ^ /%1 [R=302,L]

# To internally forward /dir/file to /dir/file.html
RewriteCond %{DOCUMENT_ROOT}/$1\.html -f
RewriteRule ^(.+?)/?$ $1.html [L]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM