本文介绍了搜索引擎友好的URL,而不是查询字符串的.htaccess的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何修改.htaccess文件,并得到搜索引擎友好的URLS,而不是查询字符串。我想要实现这个目标3:

  1. 本地主机/例子/产品/ 代替本地主机/例子/产品-list.php的
  2. 而不是本地主机/例子/产品/ 38 / 本地主机/例子/ products.php?ID = 38
  3. 本地主机/例子/产品/ 38 /红/ 代替本地主机/例子/ products.php ID = 38&安培;颜色=红色

在另一篇文章中@anubhava帮了我很多,这就是我现在所拥有的第二点:

  RewriteEngine叙述上
的RewriteBase /例子
#外部重定向从实际的URL pretty的1
的RewriteCond%{} THE_REQUEST /products(?:\.php)?\?id=([^\s&]+)[NC]
重写规则^产品/%1? [R = 302,L,NE]
从pretty的URL#内部着实际单
的RewriteCond%{} REQUEST_FILENAME!-d
的RewriteCond%{} REQUEST_FILENAME!-f
重写规则^产品/([^ /] +)/ $ products.php?ID = $ 1 [L,QSA]
 

第二点工作正常,但我想知道我必须把其他规则,之前或之后。我把其他的规则以后这个权利,但它不工作,因为它重定向到previous URL的本地主机/例子/产品/ 38 / 的:

 #外部重定向从实际的URL pretty的1
的RewriteCond%{} THE_REQUEST /products(?:\.php)?\?id=([^\s&]+)\?color=([^\s&]+)[NC]
重写规则^产品/ 1%/ 2%? [R = 302,L,NE]
从pretty的URL#内部着实际单
的RewriteCond%{} REQUEST_FILENAME!-d
的RewriteCond%{} REQUEST_FILENAME!-f
重写规则^产品/([^ /] +)1 /([^ /] +)/ $ products.php ID = $&放大器;?色= $ 2 [L,QSA]
 

解决方案

您需要一套新的规则,2个参数:

  RewriteEngine叙述上
的RewriteBase /例子/

#外部重定向从实际的URL pretty的1
的RewriteCond%{} THE_REQUEST /products(?:\.php)?\?id=([^\s&]+)&color=([^\s&]+)[NC]
重写规则^产品/ 1%/ 2%/? [R = 302,L,NE]

的RewriteCond%{} THE_REQUEST /products(?:\.php)?\?id=([^\s&]+)\s [NC]
重写规则^产品/%1 /? [R = 302,L,NE]

的RewriteCond%{} REQUEST_FILENAME -D [OR]
的RewriteCond%{} REQUEST_FILENAME -f
重写规则^  -  [L]

从pretty的URL#内部着实际单
重写规则^产品/([^ /] +)/ $ products.php?ID = $ 1 [NC,L,QSA]

重写规则^产品/([^ /] +)1 /([^ /] +)/ $ products.php ID = $&放大器;?色= $ 2 [NC,L,QSA]

重写规则^产品/?$产品-list.php的[L,NC]
 

How can I modify the .htaccess file and get SEO friendly URLS instead of a query string. I want to achieve this 3 goals:

  1. localhost/example/products/ instead oflocalhost/example/products-list.php
  2. localhost/example/products/38/ instead oflocalhost/example/products.php?id=38
  3. localhost/example/products/38/red/ instead oflocalhost/example/products.php?id=38&color=red

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]

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]
解决方案

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]

这篇关于搜索引擎友好的URL,而不是查询字符串的.htaccess的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 07:50