【问题标题】:htacces rewriting for url with ? & id'shtaccess 用 ? 重写 url &身份证
【发布时间】:2013-11-12 11:46:22
【问题描述】:

我遇到了一个棘手的情况,我需要用“?”重写一个 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。有什么可能吗?

【问题讨论】:

    标签: wordpress apache .htaccess url seo


    【解决方案1】:

    试试这个:

    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

    【讨论】:

    【解决方案2】:

    这是针对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] 
    

    【讨论】:

      猜你喜欢
      • 2011-10-25
      • 1970-01-01
      • 2020-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-06
      相关资源
      最近更新 更多