首页 文章

RewriteEngine不会影响URL重写

提问于
浏览
1

我正在尝试进行简单的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,而不仅仅是重写它 .

1 回答

  • 0

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

    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]
    

相关问题