寻找一个重写规则的解决方案



我有一个url重写问题。我网站的所有主题网址都指向www.mywebsite.com/topic.php?topic=id我想把这些url改为www.mywebsite.com/topic-id.html

我在一个。htaccess文件中这样做了:

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^topic-([0-9]+).html$ /topic.php?topic=$1

现在我想所有的url指向/topic.php?Topic =id要像Topic -id.html一样重写,以便只有一个url显示给用户,而不重写我所有的代码。可以吗?

我尝试了[R]标志:

RewriteRule ^topic-([0-9]+).html$ /topic.php?topic=$1 [R]

但它正在做什么我正在寻找的相反,因为它转换所有的url,如(topic-id.html)到(topic.php?主题= id)。

我指定,我不是在寻找"重复内容问题"的解决方案

任何帮助将是伟大的,谢谢你

你可以试试这个,我希望这对你有用

RewriteCond %{THE_REQUEST} s/topic.php?id=([_0-9a-zA-Z-]+)s [NC]
RewriteRule ^%1? [R=301,L]
RewriteRule ^([_0-9a-zA-Z-]+)$ /topic.php?id=$1 [L]

RewriteCond %{THE_REQUEST} s/topic.php?id=([_0-9a-zA-Z-]+)s [NC]
RewriteRule ^ %1.html? [R=301,L]
RewriteRule ^([_0-9a-zA-Z-]+?)(?:.html)?$ /index.php?id=$1 [NC,L]

将以下内容置于现有规则之上(RewriteEngine on行以下):

RewriteCond %{THE_REQUEST} /topic.php?id=(.+)sHTTP [NC]
RewriteRule ^ /topic-%1.html [L,R]

你需要使用的是:

RewriteEngine On
RewriteRule ^topic-([^-]*).html$ /topic.php?topic=$1 [L]

最新更新