简体   繁体   中英

rewrite rule to redirect to SEO friendly URL

I am creating a SEO friendly URL for a page that has query string using rewrite code below

RewriteRule certificates/finance curr/certificate.php?id=26 [L,QSA]

but now I want to redirect from non friendly URL ie curr/certificate.php?id=26 to friendlier one ie certificates/finance if someone comes to non-friendlier URL

I tried adding following code

RewriteCond %{QUERY_STRING} id=26
RewriteRule dir1/xyz.php /certificates/finance? [R=301,L]

but then I get too many redirects error. Is there a way to create a SEO friendly url and make users go to that URL if they come to non SEO friendly URL.

Thanks for any help.

You need to match against %{THE_REQUEST} variable instead of %{QUERY_STRING} . Since you first redirect and then rewrite the same request your internal RewriteRule conflicts with the Rule with R flag and causes an infinite loop error.

RewriteCond %{THE_REQUEST} /curr/certificate.php\?id=26\s [NC]
RewriteRule ^ /certificate/finance? [L,R]
RewriteRule certificates/finance curr/certificate.php?id=26 [L,QSA]

If your apache version is 2.4 , you can also use END flag in your RewriteRule to immediately terminate the rewrite processing . This will avoid the infinite loop error when redirecting and rewriting the same request.

RewriteCond %{QUERY_STRING} ^id=26$ [NC]
RewriteRule ^curr/certificate.php$ /certificate/finance? [L,R]
RewriteRule certificates/finance curr/certificate.php?id=26 [QSA,END]

Clear your browser cache before testing these new redirects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM