使用.haccess重定向到特殊路由

问题描述 投票:0回答:1

我想从

example.com
重定向到
example.com/home
。默认的 .haccess 文件是: 为什么下面的代码不起作用?

# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php81” package as the default “PHP” programming language.
<IfModule mime_module>
  AddHandler application/x-httpd-ea-php81 .php .php8 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit.

为了重定向到

home
路线,我将以下代码添加到
.haccess

# Redirect to example.com/home
<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /
    RewriteRule ^index\.html$ - [L]
    RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www\.example\.com$
    RewriteRule ^(.*)$ "http://example.com/home" [R=301,L]
</IfModule>

现在,.haccess 文件内容是

# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php81” package as the default “PHP” programming language.
<IfModule mime_module>
  AddHandler application/x-httpd-ea-php81 .php .php8 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit.

# Redirect to example.com/home
<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /
    RewriteRule ^index\.html$ - [L]
    RewriteCond %{HTTP_HOST} ^example\.ir$ [OR]
    RewriteCond %{HTTP_HOST} ^www\.example\.ir$
    RewriteRule ^(.*)$ "http://example.com/home" [R=301,L]
</IfModule>

但它不起作用,我遇到以下错误:

Internal Server Error 服务器遇到内部错误或 配置错误,无法完成您的请求。

如何从

example.com
重定向到
example.com/home

我在网上查了一下。并发现以下问题。但这是我的问题。 想要将 URL 从 www.domain.com/home 更改为 www.domain.com

.htaccess
1个回答
0
投票

@CBroe解决了我的问题。

@CBroe评论:

^(.*)$
匹配任何可能的 URL 路径,因此它也会重定向
/foobar
,当然还会再次重定向
/home
。您想要检查一个空的 URL 路径,因此它只是 ^$。

© www.soinside.com 2019 - 2024. All rights reserved.