SEO friendly URL instead of query string .htaccess

后端 未结 1 1467
清酒与你
清酒与你 2021-01-16 14:01

How can I modify the .htaccess file and get SEO friendly URLS instead of a query string. I want to achieve this 3 goals:

  1. localhost/example/products/
相关标签:
1条回答
  • 2021-01-16 14:38

    You need new set of rules for 2 parameters:

    RewriteEngine on
    RewriteBase /example/
    
    # external redirect from actual URL to pretty one
    RewriteCond %{THE_REQUEST} /products(?:\.php)?\?id=([^\s&]+)&color=([^\s&]+) [NC]
    RewriteRule ^ products/%1/%2/? [R=302,L,NE]
    
    RewriteCond %{THE_REQUEST} /products(?:\.php)?\?id=([^\s&]+)\s [NC]
    RewriteRule ^ products/%1/? [R=302,L,NE]
    
    RewriteCond %{REQUEST_FILENAME} -d [OR]
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ - [L]
    
    # internal forward from pretty URL to actual one
    RewriteRule ^products/([^/]+)/?$ products.php?id=$1 [NC,L,QSA]
    
    RewriteRule ^products/([^/]+)/([^/]+)/?$ products.php?id=$1&color=$2 [NC,L,QSA]
    
    RewriteRule ^products/?$ products-list.php [L,NC]
    
    0 讨论(0)
提交回复
热议问题