SEO friendly URL instead of query string .htaccess(SEO 友好 URL 而不是查询字符串 .htaccess)
问题描述
如何修改 .htaccess 文件并获取 SEO 友好的 URL 而不是查询字符串.我想实现这三个目标:
How can I modify the .htaccess file and get SEO friendly URLS instead of a query string. I want to achieve this 3 goals:
- 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
- localhost/example/products/ instead of localhost/example/products-list.php
- localhost/example/products/38/ instead of localhost/example/products.php?id=38
- localhost/example/products/38/red/ instead of localhost/example/products.php?id=38&color=red
在另一篇文章中@anubhava 帮了我很多,这就是我现在所拥有的第二点:
On another post @anubhava helped me a lot and this is what I have right now for the second point:
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/:
The second point works properly but I want to know where do I have to put the other rules, before or after. I put this right after the other rule, but it's not working because it redirects to the previous 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 个参数规则:
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]
这篇关于SEO 友好 URL 而不是查询字符串 .htaccess的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:SEO 友好 URL 而不是查询字符串 .htaccess


- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- PHP - if 语句中的倒序 2021-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01