SEO 友好 URL 而不是查询字符串 .htaccess
SEO friendly URL instead of query string .htaccess
如何修改 .htaccess 文件并获取 SEO 友好的 URL 而不是查询字符串。我想实现这 3 个目标:
- localhost/example/products/ 而不是
localhost/example/products-list.php
- localhost/example/products/38/ 而不是
localhost/example/products.php?id=38
- localhost/example/products/38/red/ 而不是
localhost/example/products.php?id=38&color=red
另一个 post @anubhava 帮了我很多,这就是我现在所拥有的第二点:
RewriteEngine on
RewriteBase /example
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /products(?:\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ products/%1? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^products/([^/]+)/?$ products.php?id= [L,QSA]
第二点工作正常,但我想知道我必须在哪里放置其他规则,之前或之后。我把它放在另一条规则之后,但它不起作用,因为它重定向到之前的 url localhost/example/products/38/:
# 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]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^products/([^/]+)/([^/]+)/?$ products.php?id=&color= [L,QSA]
您需要为 2 个参数设置新的规则:
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= [NC,L,QSA]
RewriteRule ^products/([^/]+)/([^/]+)/?$ products.php?id=&color= [NC,L,QSA]
RewriteRule ^products/?$ products-list.php [L,NC]
如何修改 .htaccess 文件并获取 SEO 友好的 URL 而不是查询字符串。我想实现这 3 个目标:
- localhost/example/products/ 而不是 localhost/example/products-list.php
- localhost/example/products/38/ 而不是 localhost/example/products.php?id=38
- localhost/example/products/38/red/ 而不是 localhost/example/products.php?id=38&color=red
另一个 post @anubhava 帮了我很多,这就是我现在所拥有的第二点:
RewriteEngine on
RewriteBase /example
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /products(?:\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ products/%1? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^products/([^/]+)/?$ products.php?id= [L,QSA]
第二点工作正常,但我想知道我必须在哪里放置其他规则,之前或之后。我把它放在另一条规则之后,但它不起作用,因为它重定向到之前的 url localhost/example/products/38/:
# 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]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^products/([^/]+)/([^/]+)/?$ products.php?id=&color= [L,QSA]
您需要为 2 个参数设置新的规则:
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= [NC,L,QSA]
RewriteRule ^products/([^/]+)/([^/]+)/?$ products.php?id=&color= [NC,L,QSA]
RewriteRule ^products/?$ products-list.php [L,NC]