0

我有一个艰难的情况,我需要用“?”重写一个 url & “id” 到一个 seo 友好的 url。

这是实际的网址
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。有什么可能吗?

4

2 回答 2

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] 

为避免重复使用每个 url 的代码,您可以使用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] 
于 2013-11-01T10:49:48.027 回答
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

于 2013-11-01T09:33:20.007 回答