doubaisui2526 2013-08-15 20:14
浏览 64
已采纳

格式化.htaccess RewriteCond / RewriteRule用于从URL中删除查询字符串

I have developed a PHP site, which uses _GET variables to construct individual product pages. The current structure of the URLs for the (live) site is as follows:

http://campbellssweets.com/shop/popcorn/product.php?subject=Bacon+and+Cheddar+Popcorn

I have 2 questions:

1) What is the proper way to construct the RewriteCond and/or RewriteRule in my .htaccess file to remove the 'product.php' and the query string from the example above?

2) Similarly, how might I go about replacing the '+' with dashes so that the final URL is displayed as:

http://campbellssweets.com/shop/popcorn/Bacon-and-Cheddar-Popcorn

For what it is worth, I am utilizing the 'index.php saved in a sub-folder' method to remove the '.php' from the category pages.

I really appreciate the help!

I have added the current work flow below to hopefully better explain my question. Apologies if I am doing something wrong here, it's my first post

I have a category page that lists all of the items in the database via a loop. The page is saved as index.php:

/shop/popcorn/index.php

Each of the items listed in the category page link to individual product pages (product.php), which is in the same directory as the category page. The (product.php) page is dynamic and its content is dependent on the value of the $_GET variable: The link from main category page that generates the product.php page is as follows:

<a href="product.php?subject=<?php echo urlencode($list_savory["product_name"]); ?>">

The product page URL's (which are the focus of this question), were displaying as:

shop/popcorn/product.php?subject=Bacon+and+Cheddar+Popcorn

I was trying to figure out the correct .htaccess code that would remove query string from the URL and replace the '+' with dashes so they would instead display as:

shop/popcorn/Bacon-and-Cheddar-Popcorn

Ravi's answer does format the URL'S in the way that I requested, but the page fails to load and I get a '404 Not Found' error - even after I successfully modified the product.php conditional to accept the $_SERVER['REQUEST_URI'] instead of the $_GET variable.

I hope that helps clear everything up. Sorry again for the confusion, I'm new at this.

  • 写回答

1条回答 默认 最新

  • douba6365 2013-08-15 23:21
    关注

    Assuming your .htaccess is in the web root / directory

    RewriteEngine on
    RewriteBase /
    
    RewriteCond %{QUERY_STRING} !no-redir [NC]
    RewriteCond %{QUERY_STRING} (^|&)subject=([^&]+) [NC]
    RewriteRule ^(.*)/product.php$ $1/%2? [NC]
    
    RewriteRule ^([^+\s]+)(?:[+\s]+)([^+\s]+)((?:[+\s]+).*)$ $1-$2$3 [DPI,N]
    RewriteRule ^([^+\s]+)(?:[+\s]+)([^+\s]+)$ $1-$2 [R=301,DPI,L]
    

    EDIT : (Parsing the URI for keywords)

    Chris, the idea is to now capture the subject from path info instead of a GET parameter because it's not being passed as ?subject=sub+info which makes it unavailable as $_GET['subject'] now.

    You'd need to get the subject keywords from the URI path as follows:

    $uri = $_SERVER['REQUEST_URI'];
    $subject = substr($uri, strrpos($, "/"));
    

    EDIT : (two-way redirection)

    Add the following after the rules defined above.

    RewriteRule ^(.*/popcorn)/([^/.]+)$ $1/product.php?subject=$2&no-redir [NC,L]
    

    Notice, I've added another RewriteCond on %{QUERY_STRING} above. This also means you can revert your product.php to the way it was before since subject is being passed again as usual.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)