【问题标题】:how to redirect old url with parameters to new seo url using htaccess如何使用 htaccess 将带有参数的旧 url 重定向到新的 seo url
【发布时间】:2018-03-24 16:42:56
【问题描述】:

想要将参数 url 重定向到 seo 友好的 url

https://example.com/a.php?slug=vr-promised-us-the-future-too-bad-we-re-stuck-in-the-present

https://example.com/vr-promised-us-the-future-too-bad-we-re-stuck-in-the-present

https://example.com/tag.php?id=Virtual-reality

https://example.com/tag/Virtual-reality

我当前的 .htaccess 代码

RewriteEngine On

RewriteRule ^([a-zA-Z0-9_-]+)/?$ a.php?slug=$1 [L]


RewriteCond %{HTTP_HOST} ^www\. [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R]

【问题讨论】:

  • 这个 htaccess 仍然是动态的(多域),并且没有域正在以困难的方式写入。所以不要更改它并使用主机管理中的 CNAME 添加站点域并添加 vhost 以指向新的本地文件夹。
  • @headmax 感谢您的回复,实际上我的问题是如何阻止重复的网址(重定向旧 => /a.php?slug=vr-promised-us-the-future-too-bad-we -re-stuck-in-the-present to /vr-promised-us-the-future-too-bad-we-re-stuck-in-the-present)

标签: php apache .htaccess mod-rewrite seo


【解决方案1】:

这样吧:

Options -MultiViews
RewriteEngine On    

RewriteCond %{HTTP_HOST} ^www\. [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R=301]

# external redirect to redirect old URL to pretty URL    
RewriteCond %{THE_REQUEST} /a\.php\?slug=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L,NE]

RewriteCond %{THE_REQUEST} /tag\.php\?id=([^\s&]+) [NC]
RewriteRule ^ /tag/%1? [R=301,L,NE]

RewriteRule ^tag/([\w-]+)/?$ tag.php?id=$1 [L,QSA,NC]

# internal rewrite to forward to actual URL
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/?$ a.php?slug=$1 [L,QSA]

【讨论】:

  • 编辑了答案。
猜你喜欢
  • 1970-01-01
  • 2022-06-11
  • 1970-01-01
  • 2015-09-14
  • 1970-01-01
  • 2017-09-25
  • 2013-03-10
  • 2014-01-30
  • 1970-01-01
相关资源
最近更新 更多