【问题标题】:redirect and change url from html to none html htaccess重定向并将 url 从 html 更改为 none html htaccess
【发布时间】:2023-03-03 13:28:01
【问题描述】:

我需要将 domain.com/abcdef.html 重定向到 domain.com/abcdef 并且新 url 的内容应该是 domain.com/abcdef.html。

我真的在这里挣扎。我可以实现从显示的 html 中获取内容,而 url 中没有 html,但是两个 url 都可用。

当我尝试同时实现时,我得到了一个无限循环和一个 ERR 500,但我不知道如何防止这种无限循环。

所以我尝试了一个 - 使两个 url 具有相同的内容看起来像这样:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ /$1 [L,R=301] 
RewriteRule ^([^\.]+)$ $1.html [NC,L]

第二条规则与前两行相同:

RewriteRule ^(.*)\.html$ /$1 [L,R=301] 
RewriteRule ^([^\.]+)$ $1.html [NC,L]

导致无限循环。我的研究只让我找到了解决方案,或者尝试使用 index.php。但我搜索的是仅 htaccess 的解决方案。

有可能吗?

编辑:我明白为什么会发生无限循环 我请求 blabla.html --> 规则 1 使其成为 blabla 并再次请求,规则 2 使其再次成为 blabla.html 请求,依此类推。但是我该如何预防呢?

编辑2:所以我按照starkeen的建议尝试了,结果如下:

RewriteEngine On
RewriteCond %{ENV_REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteBase /
RewriteRule ^(.*)\.html$ /$1 [L,R=301] 
RewriteRule ^([^\.]+)$ $1.html [NC,L]

这仍然会导致无限循环。将它与 RewriteCond 一起使用仍然可以使用(blabla.com/content.html 和 blabla.com/content)

不幸的是,它不是 apache 2.4,所以我无法尝试使用 END 标签,但我尝试联系我的主机并询问他们是否更新了它。

还是我弄错了,把 htaccess 搞混了?

编辑 3:因此,由于我的代码应该可以工作,但不能正常工作,所以这里是整个 htaccess,以确保没有其他任何东西阻止 **** 工作。

AuthName "nottellingyou" 
AuthType Basic 
AuthUserFile /home/www/top/secret/.htpasswd 
AuthGroupFile /dev/null 
require valid-user

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{ENV_REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteBase /
RewriteRule ^(.*)\.html$ /$1 [L,R=301] 
RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

【问题讨论】:

    标签: apache .htaccess


    【解决方案1】:

    为了防止循环错误,将以下规则放在 RewriteEngine On

    RewriteCond %{ENV_REDIRECT_STATUS} 200
    RewriteRule ^ - [L]
    

    这会在第二次迭代中终止内部重写处理,因此 /file.html 不会再次被重写到相同的路径。

    在 apach 2.4 上,您可以在 RewriteRule 中使用 END 标志来防止内部循环

    RewriteRule ^([^.]+) $1.html [END]
    

    要删除 .html 扩展名,您也可以使用此脚本:

    RewriteEngine on
    
    
    #1)redirect /file.html to /file
    
    RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
    RewriteRule ^ /%1 [NE,L,R]
     #2)check if /file exists as an .html ,if it exists ,rewrite /file to /file.html
    RewriteCond %{REQUEST_FILENAME}.html -f
    
    RewriteRule ^(.*?)/?$ /$1.html [L]
    

    【讨论】:

    • 嘿,感谢您的帮助,但不幸的是它对我不起作用!如果我做得对,请检查我的编辑吗?
    • @marcel 您没有正确使用该规则。应该是下面的 RewriteEngine 上线了。
    • 非常感谢,我像以前一样误读了 RewriteEnginge。但不幸的是同样的效果。 (editedhow 现在看起来像 --> 导致我循环)
    • 是的,我尝试了 inkognito 模式并使用开放的开发人员工具和禁用的缓存刷新。它也从工作切换到不工作,这应该意味着它没有缓存
    • 有线。此代码在我的服务器上运行良好。你在htaccess中还有其他规则吗?
    【解决方案2】:

    如果您的网站具有类似 magepow.com/hello-world.html 1 的 URL,并且您想将其重定向到 magepow.com/hello-world/,那么您可以尝试以下代码。将其复制/粘贴到您的 .htaccess 文件中。确保将 magepow.com 1 替换为您自己的 URL。

    RedirectMatch 301 ^/([^/]+).html$ http://magepow.com/$1
    

    这应该可以正常工作,如果您的网站使用“HTTPS”而不是“HTTP”,也请更改它。

    注意:如果上述代码不适用于您的网站,您可以尝试以下方法:

    RedirectMatch 301 ^/([^/]+)/([^/.]+).html$ /$1/$2/
    RedirectMatch 301 ^/([^/]+)/([^/]+)/([^/.]+).html$ /$1/$2/$3/
    RedirectMatch 301 ^/([^/]+)/([^/]+)/([^/.]+).html$ /$1/$2/$3/$4/
    

    【讨论】:

      猜你喜欢
      • 2012-04-27
      • 1970-01-01
      • 1970-01-01
      • 2015-02-04
      • 1970-01-01
      • 2015-10-22
      • 2019-02-21
      • 1970-01-01
      • 2023-03-03
      相关资源
      最近更新 更多