格式化的.htaccess的RewriteCond /重写规则从删除的网址查询字符串重写、字符串、规则、网址

2023-09-02 01:15:58 作者:款少爷

我已经开发了一个PHP的网站,它使用_ GET变量来构建单个产品页面。网址为(活)的网站目前的结构如下:

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

我有2个问题:

1)什么是构造在我的.htaccess文件删除product.php,并从上面的例子中,查询字符串的RewriteCond和/或重写规则的正确方法?

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)同样的,怎么可能我去用破折号代替'+',这样最终的URL显示为:

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-切达-爆米花

对于什么是值得的,我利用的index.php保存在子文件夹的方法,从分类页面删除'的.php。的

我真的AP preciate的帮助!

I really appreciate the help!

我已经加入下面希望更好地解释我的问题在目前的工作流程。道歉,如果我在这里做得不对,这是我的第一篇文章

我有一个类别页面其经由循环列出所有的数据库中的项目。该页面将被保存为的index.php:

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

每个在分类页面链接到各个产品页面(product.php),这是在相同的目录分类页面列出的项目。在(product.php)页面是动态的,它的内容依赖于$ _ GET变量的值:即产生product.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"]); ?>">

该产品页面的URL(这是这个问题的焦点),分别显示为:

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

店/爆米花/ product.php?主题=培根+和+切达+爆米花

我试图找出正确的.htaccess code,它会从URL中删除查询字符串,用破折号代替'+',这样他们就改为显示为:

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:

店/爆米花/培根和-切德 - 爆米花

拉​​维的回答确实格式化的URL中,我要求的方式,但网页加载失败,我得到一个404未找​​到的错误 - 即使我成功修改了product.php有条件接受$ _ SERVER [' REQUEST_URI]而不是$ _GET变量。

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.

推荐答案

假设你的的.htaccess 是Web根 / 目录

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]

修改:(解析URI关键字)的

EDIT : (Parsing the URI for keywords)

克里斯,这个想法是现在捕获路径信息,而不是 GET 参数的问题,因为它没有被通过为?主题=子+信息这使得它无法为 $ _ GET ['主题'] 了。

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.

您会需要从URI路径的主题关键字如下:

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]

注意,我已经增加了一个的RewriteCond %{QUERY_STRING} 上方。这也意味着你可以恢复你的 product.php 来的方式是之前因为主题被再次传递如常

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.