我想将 example.com/chapter-foo.php 重定向到 example.com/chapter.php

保克雷

我有一个网站,里面有一些这样的页面:

  • example.com/dogs-foo1.php
  • example.com/dogs-foo2.php
  • example.com/dogs-foo3.php

然后

  • example.com/cats-foo1.php
  • example.com/cats-foo2.php
  • example.com/cats-foo3.php

现在我用标签菜单简化了网站,我只有

  • example.com/dogs.php
  • example.com/cats.php

现在我希望尝试访问:example.com/cats-foo1.php 的人被重定向到:example.com/cats.php 而不是获得 404 无论如何,也许使用 htaccess?

阿卡沙

这可能就是你要找的:

RewriteEngine on 
RewriteRule ^/?cats-.+.php$ /cats.php [R=301]

为此,您需要在 http 服务器中启用重写模块。您应该将它放在 http 服务器主机配置中。如果您决定使用动态配置文件 ( .htaccess),您需要先启用它们的解释(请参阅AllowOverride官方文档中指令)。


然而,我建议更进一步并沿模式使用 URL https://example.com/cats,因此没有.php像现在标准那样的尾随为此,您需要一些额外的内部重写规则:

RewriteEngine on 
# external redirect from /cats-foo.php to /cats
RewriteRule ^/?cats-.+\.php$ /cats [R=301]
RewriteRule ^/?cats\.php$ /cats [R=301]
# internal rewrite 
RewriteRule ^/?cat$ /cats.php [END]

这再次可以概括:

RewriteEngine on 
# external redirect from /cats-foo.php to /cats
RewriteRule ^/?(\w+])-.+.php$ /$1 [R=301]
# internal rewrite 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(\w+])$ /$1.php [END]

如果您在使用该规则的情况下遇到 http 状态 500,则您可能运行的是非常旧版本的 apache http 服务器。您会在 http 服务器错误日志文件中找到抱怨该END标志的条目在这种情况下,将其替换为L标志并重试。

您可能需要调整这些线路,我们不知道您的具体情况。但是上面应该让你开始阅读你使用的工具的文档,你可以从这里开始:http : //httpd.apache.org/docs/current/mod/mod_rewrite.html


还有一个一般提示:您应该始终更喜欢将此类规则放置在 http 服务器(虚拟)主机配置中,而不是使用动态配置文件(.htaccess样式文件)。众所周知,这些文件容易出错,难以调试,而且确实会降低服务器的速度。它们仅在您无法控制主机配置的情况下(阅读:非常便宜的托管服务提供商)或如果您的应用程序依赖于编写自己的重写规则(这是一个明显的安全噩梦) )。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

从example.com/foo.php永久重定向到example.com/foo分别从example.com/foo.php到example.com/bar

Joomla:永久从example.com/index.php/foo/bar重定向到example.com/foo/bar(URL中没有index.php)

为什么我的网站被重定向到example.com?

当我在地址栏中按回车键时,Laravel 6 重定向到 https://www.example.com/index.php,地址为 www.example.com/profile?

PHP重定向到自我

.htaccess重定向到example.com,但目录重定向到example.com/example

我如何使用 .htaccess 重定向像 (https://example.com/digital-marketing.php/ercbierubcrei) 这样的非结构化链接?

我应使用哪种方法将example.com定向到www.example.com-重定向.htaccess或DNS记录?

.htaccess重定向到`www.www.example.com`

Carousel Slider重定向到#carousel-example-generic

www.example.com 没有重定向到 HTTPS

使用JavaScript重定向到(currenturl)/example.html onclick

Django如何仅将example.com重定向到https并将* .example.com重定向到http?

如何从http重定向到https和example.com重定向到www.example.com

Web.config重定向到http://www.example.com到https://example.com

我如何根据基本php文件中的按下按钮重定向到不同的php文件?

Heroku从example.com重定向到www.example.com,使用AWS保留HTTPS

使用 github 页面将 www.example.com 重定向到 example.com?

从example.com/永久重定向到https://www.example.com/dir/

从www.example.com重定向到www.example.com/index.jsp

将http://www.example.com重定向到http://example.com

使用VirtualDocumentRoot将www.example.com重定向到example.com

IHttpHandler将“ https://example.com”重定向到“ https://www.example.com”吗?

Firebase托管将www.example.com重定向到example.com

无法将sub.example.com重定向到sub.example.com/directory/

将 www.*.example.com 重定向到 *.example.com

将example.com上的每个页面重定向到example.org

将https://example.com重定向到https://www.example.com Heroku / Namecheap

将example.com重定向到example.com/blog