htaccess重写为url? &安培; IDS

时间:2013-11-01 09:26:31

标签: wordpress apache .htaccess url seo

我遇到了困难,需要用“?”重写网址。 &安培; “id”到一个友好的网址。

这是实际的网址 http://www.example.com/services.html?sr=1
http://www.example.com/services.html?sr=2
http://www.example.com/services.html?sr=3
http://www.example.com/services.html?sr=4

我需要把它改成这样的东西 http://www.example.com/services/car-dealers.html
http://www.example.com/services/restaurent.html
http://www.example.com/services/nonprofit.html
http://www.example.com/services/school.html

我的htacces代码

    <IfModule mod_rewrite.c>

      RewriteEngine On

      RewriteRule ^services/car-dealers.html services.html?sr=1 [L]
      RewriteRule ^services/restaurent.html services.html?sr=2 [L]
      RewriteRule ^services/nonprofit.html services.html?sr=3 [L]
      RewriteRule ^services/school.html services.html?sr=4 [L]

      RewriteBase /
      RewriteRule ^index\.php$ - [L]
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule . /index.php [L]

    </IfModule>

我正在使用WordPress作为CMS。有可能吗?

2 个答案:

答案 0 :(得分:0)

试试这个:

RewriteEngine on

RewriteRule ^services/car-dealers services.html?sr=1 [NC] 
RewriteRule ^services/restaurent services.html?sr=2 [NC]
RewriteRule ^services/nonprofit services.html?sr=3 [NC]
RewriteRule ^services/school services.html?sr=4 [NC]

访问http://www.example.com/services/car-dealers

答案 1 :(得分:0)

这是针对案例sr=1实现此目标的解决方案,对其他网址执行相同操作

 RewriteCond %{ENV:REDIRECT_STATUS} !200
 RewriteCond %{QUERY_STRING} ^sr=1$
 RewriteRule ^services\.html$ /services/car-dealers.html [NC,R=301,L]

 RewriteRule ^services/car-dealers services.html?sr=1 [L,NC] 

为避免您可以使用RewriteMap的每个网址反复重复代码,首先要创建一个包含键值对的txt文件,如下所示:

car-dealers 1
restaurent 2
nonprofit 3
school 4
1 car-dealers
2 restaurent 
3 nonprofit 
4 school

然后在你的htaccess中:

 RewriteMap name2id txt:/etc/apache2/name2idmap.txt

 RewriteCond %{ENV:REDIRECT_STATUS} !200
 RewriteCond %{QUERY_STRING} ^sr=([0-9]+)$
 RewriteRule ^services\.html$ /services/${name2id:%1} [NC,R=301,L]

 RewriteRule ^services/(.+?)\.html services.html?sr=${name2id:$1} [L,NC]