0
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*)\.html$ fetch.php?id=$1

I have this in my htaccess file and it works fine, but I want to know if there is a way to rewrite this so that instead of just pulling the id # i can pull the category and title up instead or will i need to do something like this

RewriteRule (.*)/(.*)/(.*)\.html$ fetch.php?id=$1&category=$2&title=$3

Also if this is what I need to do, how will I go about changing my title to have no spaces and only have dashes inbetween them? I heard that urls don't like spaces. I have read somewhere else they used a php code where the spaces were turned into "-" or "_" to be read into the page but reverted back to be read in sql. Let me know if you need to know more about my situation. Thanks.

UPDATE!

RewriteRule (.*)/(.*)/(.*)\.html$ fetch.php?id=$1&category=$2&title=$3

This is the htaccess rule

<a href="<?php echo $row_getDisplay['id']; ?>/<?php echo $row_getDisplay['category']; ?>/<?php echo urlencode($row_getDisplay['title']); ?>">

that is my link .. this method I get it to pull up with id/category/title with no spaces but it has no css or images displaying. i'm pulling by ID because that's how my fetch.php is pulling it i don't know how to set it so it pulls by category and title.

4

2 回答 2

0

我建议不要这样做,因为这不是很可扩展,您可能想尝试以下方法:

在您的 htaccess 中添加以下内容:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* fetch.php/%{REQUEST_URI} [QSA,L]

这会将您的整个 url 放入 path_info 环境变量中,然后您可以在后端代码中使用它。

RewriteCond 部分确保只有在请求的文件实际上不存在时才会重写 url。

于 2012-08-14T13:17:16.250 回答
0

这种方法我得到它以没有空格的 id/category/title 拉起,但它没有显示 css 或图像。我按 ID 拉,因为这就是我的 fetch.php 拉它的方式,我不知道如何设置它,所以它按类别和标题拉。

这可能是因为您的所有链接都是相对的,并且基本 URI 已从/something.html(ie /)更改为 ( /something/foo/bar.htmlie /something/foo/),因此所有相对链接现在都已断开。尝试将其添加到页面的标题中:

<base href="/">
于 2012-08-14T17:38:44.583 回答