首页 > 解决方案 > 在 htaccess 中使用 Rewriterule 的问题

问题描述

使用 RewriteRule 我总是按如下方式清理我的 URL:

RewriteRule ^page/(.*)$ page.php?urlkey=$1 [QSA]

我的新主机不允许我使用 Options +FollowSymLinks,因此我不能再使用 /。因此,我将 RewriteRule 更改为:

RewriteRule ^page-(.*)$ page.php?urlkey=$1 [QSA]

但是,我需要将所有以前的 URL 重定向到新版本。我尝试使用以下规则执行此操作:

RewriteRule ^page/(.*)$ page-(.*)$ [R=301,L]

然而,这是行不通的。我还尝试在我的 .htaccess 中进行重定向:

Redirect 301 https://www.example.com/page/urlkey https://www.example.com/page-urlkey

这也行不通。

编辑

根据要求,下面的实际代码:

RewriteEngine on 
RewriteRule ^citywalk-(.*)$ citywalk.php?urlkey=$1 [QSA]
RewriteRule ^citywalk/(.*)$ citywalk-(.*)$ [R=301,L]

例如 citywalk The Historical Center 有一个 urlkey the-historical-center。旧网址是 citywalk/the-historical-center。

要测试此特定案例和其他技术:

Redirect 301 /citywalk/the-historical-centre https://example.com/citywalk-the-historical-centre

通过访问https://example.com/citywalk/the-historical-centre不会发生重定向(浏览器中的 url 保持不变)并且没有找到 urlkey。

标签: .htaccessmod-rewrite

解决方案


推荐阅读