如何将两个参数URL重写为SEO友好URL并重定向到SEO友好URL

时间:2018-01-23 04:58:55

标签: apache .htaccess url-rewriting url-routing

这是我的网页网址example.com/platform/bidProject.php?pID=JCVGK&name=Proof%20Reading%20Blogs

这些规则帮助我将其重写为example.com/platform/project-bids/JCVGK/Proof-Reading-Blogs/

RewriteRule ^platform/project-bids/(.*)/(.*)/?$ /platform/bidProject.php?pID=$1&name=$2 [L,NC]
RewriteCond %{THE_REQUEST} \s/platform/bidProject.php?pID=$1&name=$2 
RewriteRule ^platform/bidProject.php?pID=$1&name=$2 /platform/project-bids/(.*)/(.*)/ [NC,R=301,L]

但问题是,如果我访问网页example.com/platform/bidProject.php?pID=JCVGK&name=Proof%20Reading%20Blogs,则网址保持不变。我希望将其重定向到example.com/platform/project-bids/JCVGK/Proof-Reading-Blogs/

所以我厌倦了这个:

RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^(\S+)$ /$1 [NE,R=302,L]

RewriteRule ^([^/]+)/([^/]+)/?$ /platform/bidProject.php?pID=$1&v=$2 [L,QSA]

似乎我对此做错了,因为它对此没有任何影响。

我找到了使用JavaScript重定向的解决方案,但我喜欢使用.htaccess解决方案,因为JavaScript可以被禁用,并且可以在源代码中看到。这样做的基本意图在这里失败了。

如何使用.htaccess

实现此目的

我的htaccess路径为example.com/.htacsess

1 个答案:

答案 0 :(得分:0)

您可以使用这些规则

RewriteEngine on
#redirects /platform/bidProject.php. php?pid=val1&name=val2 to /platform/project-bids/val1/val2/
#redirects the old url to the new one
RewriteCond %{THE_REQUEST} \s/platform/bidProject.php\?pID=([^&]+)&name=([^&\s]+) [NC]
RewriteRule ^ /platform/project-bids/%1/%2/? [NC,R,L]
# rewrites or internally maps the new url to the old one
RewriteRule ^platform/project-bids/(.*)/(.*)/?$ /platform/bidProject.php?pID=$1&name=$2 [L,NC]

或者

RewriteEngine on
RewriteCond %{ENV_REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^pid=([^&]+)&name=([^&]+)$ [NC]

RewriteRule ^platform/bidProject.php$ /platform/project-bids/%1/%2/? [NC,R,L]
RewriteRule ^platform/project-bids/(.*)/(.*)/?$ /platform/bidProject.php?pID=$1&name=$2 [L,NC]

当您确定规则正常时,将R更改为R=301(永久重定向)。