301 redirect query string to SEO friendly URLs through .htaccess

前端 未结 2 1672
予麋鹿
予麋鹿 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: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.

提交回复
热议问题