doujunchi1238 2014-05-05 13:06
浏览 49
已采纳

301重定向和URL在同一规则中重写

I need to rewrite urls and redirect the old requests to the new urls permanently. It's important to get the 301 recognized by Google to maintain the indexed pages.

Old Url: example.com/buy-productname-category-item/rest-of-url.html

New Url: example.com/productname-item/rest-of-url.html

Basically removing "buy-" and "category-" from the first part of the url. The rest of the urls all follow the same structure.

Options +FollowSymLinks
RewriteEngine On


# this rule sends root requests to index.html, which is then sent by the next rule to /php/page.php 

# sends all requests for .html files to page.php for service
RewriteCond %{REQUEST_URI} !(.*)/sitemap.xml
RewriteCond %{REQUEST_URI} !(.*)/xml
RewriteCond %{REQUEST_URI} !(.*)/mockups
RewriteCond %{REQUEST_URI} !(.*)/php
RewriteCond %{REQUEST_URI} !(.*)/rss
RewriteRule ^(.*).html$ /php/page.php?uri=$1 [NC]
### ^ the most important rule

#---- remove buy- and ABC from urls
# the uri is being changed and page.php can't find it so it handles it as 404
RewriteRule buy-(.+)-ABC-123/(.+)$ /$1-123/$2 [L,R=301]

Please let me know why the above code is not working and also let me know if the [L,R=301] is actually what I need here.

  • 写回答

1条回答 默认 最新

  • doufu4333 2014-05-05 13:21
    关注

    You can try this

    RewriteEngine On
    RewriteBase / #modify rewrite base according to your directory structure
    RewriteRule buy-(.+)-category-(.+)/(.+)$ /$1-$2/$3 [L,R=301]
    

    Update

    I think if you place the above rewrite statements above your original .htaccess code (after rewriteengine on), it'll map your request to the right page, i.e., /page/page.php with your URL in $_GET['uri'], i.e., [uri] => productname-item/rest-of-url.html. In that page you can parse the URL (like probably you're already doing) and extract the values.

    Final .htaccess code

    Options +FollowSymLinks
    RewriteEngine On
    
    
    # ######### new code ###
    RewriteBase / #modify rewrite base according to your directory structure
    RewriteRule buy-(.+)-category-(.+)/(.+)$ /$1-$2/$3 [L,R=301]
    # ######################
    
    
    # this rule sends root requests to index.html, which is then sent by the next rule to /php/page.php 
    # sends all requests for .html files to page.php for service
    RewriteCond %{REQUEST_URI} !(.*)/sitemap.xml
    RewriteCond %{REQUEST_URI} !(.*)/xml
    RewriteCond %{REQUEST_URI} !(.*)/mockups
    RewriteCond %{REQUEST_URI} !(.*)/php
    RewriteCond %{REQUEST_URI} !(.*)/rss
    RewriteRule ^(.*).html$ /php/page.php?uri=$1 [NC]
    ### ^ the most important rule
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法