首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用htaccess将带有参数的旧url重定向到新seo url

如何使用htaccess将带有参数的旧url重定向到新seo url
EN

Stack Overflow用户
提问于 2017-10-12 16:37:29
回答 1查看 601关注 0票数 2

要将param 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代码

代码语言:javascript
复制
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]
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-12 16:51:24

以这种方式:

代码语言:javascript
复制
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]
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46714778

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档