Php 搜索引擎优化友好的URL,而不是查询字符串.htaccess

Php 搜索引擎优化友好的URL,而不是查询字符串.htaccess,php,.htaccess,url-rewriting,seo,clean-urls,Php,.htaccess,Url Rewriting,Seo,Clean Urls,如何修改.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.p

如何修改.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 在另一篇帖子上@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=$1 [L,QSA]
第二点工作正常,但我想知道我必须把其他规则放在哪里,在之前还是之后。我把它放在另一条规则之后,但它不起作用,因为它重定向到前面的url localhost/example/products/38/:


您需要两个参数的新规则集:

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]

您需要两个参数的新规则集:

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]

当你在论坛上提问时,你应该是令人愉快的,并且在提问之后,你应该付出努力,以便有人会帮助你。我已经为您更新了问题。@Sagar:…我将其回滚,因为您没有改进问题,并且您毫无意义地添加了一个“请帮助我”样式,用于编辑我的问题。在其他帖子上,我提前表示了感谢,有人删除了它,认为这是错误的。@Estirpicidades:这是错误的,你不应该把它放回去。与其继续Sagar无用的改进,不如用一些有意义的信息来编辑它,特别是尝试一些东西并展示你的努力。如果你像这样离开,你不会得到任何帮助,只是没有具体的内容。谢谢@musefan,很抱歉我的问题,我是新来的,我会努力改进。在论坛上提问时,你应该感到高兴,并且在提问之后,你应该付出努力,以便有人能提供帮助。我已经为您更新了问题。@Sagar:…我将其回滚,因为您没有改进问题,并且您毫无意义地添加了一个“请帮助我”样式,用于编辑我的问题。在其他帖子上,我提前表示了感谢,有人删除了它,认为这是错误的。@Estirpicidades:这是错误的,你不应该把它放回去。与其继续Sagar无用的改进,不如用一些有意义的信息来编辑它,特别是尝试一些东西并展示你的努力。如果你像这样离开它,你将得不到任何帮助,它只是不具体。谢谢@musefan,很抱歉我的问题,我是新来的,我会努力改进。