简体   繁体   English

.htaccess重定向和可见URL

[英].htaccess redirect and visible URL

I'm new to htaccess and I want to know how can I do this two things and if they are recommended or if is there another better way to achieve a SEO friendly and nice URL: 我是htaccess的新手,我想知道我该怎么做这两件事,以及是否建议使用它们,或者是否还有另一种更好的方法来实现SEO友好且美观的URL:

  1. Redirect from www.example.com/products?id=car to example.com/car . 从www.example.com/products?id=car重定向到example.com/car。 If you can tell me the best method for SEO friendly it would be great (301 or whatever). 如果您能告诉我SEO友好的最佳方法,那就太好了(301或其他方法)。
  2. Show always the url example.com/car as a pretty url and not with symbols, even when clicking a link that redirects to www.example.com/products?id=car. 即使单击重定向到www.example.com/products?id=car的链接,也始终将URL example.com/car显示为漂亮的URL,而不显示符号。

You can use this code in your DOCUMENT_ROOT/.htaccess file: 您可以在DOCUMENT_ROOT/.htaccess文件中使用以下代码:

RewriteEngine On
RewriteBase /example/

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /products(?:\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L,NE]

# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ products.php?id=$1 [L,QSA]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM