繁体   English   中英

RewriteEngine不会影响URL重写

[英]RewriteEngine doesn't affect URL rewriting

我正在尝试进行简单的URL重写(例如:“ / toto.html”将在URL栏中显示为“ / toto”)。 我的.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]

它确实起作用:'/toto.html'被重写为'/ toto'。 但是,服务器一直在尝试将我重定向到不带.html扩展名的页面,而不是简单地重写它。 我已经阅读了很多其他有关RewriteEngine被忽略等问题的文献记录,并且尝试了很多事情,但都没有成功。 服务器仍在尝试解释URL,而不仅仅是重写URL。

您需要一个重定向规则和一个重写规则:

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