301 redirect query string to SEO friendly URLs through .htaccess

前端 未结 2 1670
予麋鹿
予麋鹿 2021-01-16 01:47

I’ve written some code on my .htaccess file which allows the use of SEO friendly URLs instead of ugly query strings. The following code rewrites the SEO friendly version in

相关标签:
2条回答
  • 2021-01-16 02:01

    Not tested, but this may work...

    RewriteRule ^directory/script.php?size=large&colour=green&pattern=(.*)$ /seo/$1/? [R=301,NE,NC,L]

    0 讨论(0)
  • 2021-01-16 02:12

    This should do the trick:

    RewriteEngine On

    ## Redirect to pretty urls
    # The '%1' in the rewrite comes from the group in the previous RewriteCond
    RewriteCond %{REQUEST_URI} !seo
    RewriteCond %{QUERY_STRING} ^size=large&colour=green&pattern=([a-zA-Z]*)$
    RewriteRule (.*) /directory\/seo\/%1\/? [L,R=301]
    
    ## Rewrite to long url, additional parameter at the end will cause
    ## the internal redirect not to match the previous rule (would cause redirect loop)
    RewriteRule ^directory\/seo\/([^/]*)/$ /directory/script.php? size=large&colour=green&pattern=$1&rewrite [L]
    

    You can also match the size and colour if needed, by changing those to regex groups as well, and using the corresponding %N

    Hope this helps.

    0 讨论(0)
提交回复
热议问题