404使用.htaccess重定向,因为它正在删除子目录

时间:2014-02-13 17:41:30

标签: regex apache .htaccess mod-rewrite redirect

我想要做的是删除.html的URL友好网站。这是我的.htaccess:

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

#example.com/page will display the contents of example.com/page.html
RewriteEngine On 
RewriteBase /rscares/

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

这就是问题,我正在子文件夹进行测试,所以如果我输入example.com/folder/page工作正常,但如果我example.com/folder/page.html它重定向到{{1我得到一个 404 ,因为该页面不存在。我想重定向到重定向之前的相同标签。有帮助吗?感谢。

1 个答案:

答案 0 :(得分:1)

您可以使用此代码:

RewriteEngine On
RewriteBase /rscares/

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

#example.com/page will display the contents of example.com/page.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+?)/?$ $1.html [L]