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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?