.htaccess - 页面未正确重定向

时间:2017-12-27 10:50:23

标签: php html .htaccess

我正在尝试将 index.html 重定向到 home.html ,并通过.htaccess从网址中删除 home.html 。但它显示页面没有正确重定向。 这意味着我需要将mydomain.com/index.html重定向到mydomain.com/home.html,我只有在访问索引页面时才需要显示mydomain.com/。

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
Redirect 301 "/index.html" "/home.html"

RewriteCond %{THE_REQUEST} ^GET.*home\.html [NC]
RewriteRule (.*?)home\.html/*(.*) /$1$2 [R=301,NE,L]

</IfModule>

2 个答案:

答案 0 :(得分:1)

您可以尝试而不是使用mod_rewrite将 DirectoryIndex 设置为所需的页面(在您的案例中为home.html):

DirectoryIndex home.html

因此,如果您致电domain.com/,它应该会为您提供domain.com/home.html的内容

供参考,Apache配置手册: https://httpd.apache.org/docs/2.4/mod/mod_dir.html “DirectoryIndex指令”

参考下面的对话:

也许这可行(对不起,我现在无法测试)

RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} =/index.html
RewriteRule (.*?)home\.html/*(.*) /$1$2 [R=301,NE,L]

一点解释:如果请求的主机与example.com匹配,并且被叫URI实际上不是真正的文件或文件夹加上URI等于 “index.html”它应该重定向到home.html。

编辑:由于您不想显示home.html,我们可以通过内部“代理”请求而不是强制外部重定向来逃避。尝试用“P”替换最后一行(RewriteRule)中的“R = 301”,所以它说RewriteRule (.*?)home\.html/*(.*) /$1$2 [P,NE,L]

答案 1 :(得分:0)

//它的工作代码我希望它有用..

<IfModule mod_rewrite.c>
RewriteEngine On
Redirect 301 / http://newsite.com/
</IfModule>

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?http://dbdenterprise\.in$
RewriteRule ^(.*)$ http://dbdenterprise.com/$1 [R=301,QSA,L]
相关问题