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 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀