gpt4 book ai didi

php - SEO 友好 URL 而不是查询字符串 .htaccess

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:32:26 27 4
gpt4 key购买 nike

如何修改 .htaccess 文件并获取 SEO 友好的 URLS 而不是查询字符串。我想实现这 3 个目标:

  1. localhost/example/products/ 而不是localhost/example/products-list.php
  2. localhost/example/products/38/ 而不是localhost/example/products.php?id=38
  3. 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/:

# 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=$1&color=$2 [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=$1 [NC,L,QSA]

RewriteRule ^products/([^/]+)/([^/]+)/?$ products.php?id=$1&color=$2 [NC,L,QSA]

RewriteRule ^products/?$ products-list.php [L,NC]

关于php - SEO 友好 URL 而不是查询字符串 .htaccess,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31180314/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com