1

我已经搜索和搜索并尝试了许多不同的东西,但它们都不起作用......这超出了我的范围。我已经尝试了至少 50 件没有运气的事情.. 有人请帮忙!

首先,我在文件夹/blog 中有一个文件“index.php”页面,该页面有一个链接,该链接将变量传递到名为“post”的页面。“post.php”页面中的 MySQL 从匹配的行中提取 MySQL 数据ID 并显示该特定帖子。

我想要做的是有一个更清洁和 SEO 友好的 URL...

例如:在“www.mydomain.com/blog/index.php”页面上,他们单击“post?title=This%20is%20a%20test%20title%20for%20the%20blog%20post...&id”之类的链接=1" 将它们带到与“/blog/”中的“index.php”页面相同目录中的“post.php”页面。

我发现的每一个 htaccess 重写都没有将 URL 更改为更友好的版本.. 一切最终都保持不变..

有人请帮忙。我希望它显示为 www.mydomain.com/blog/title 而不是显示上述内容!如果有人可以帮助我删除帖子标题中出现的空格“%”并用“-”替换,那也很棒..

::绝望:: 再次感谢,亚当

目前在位于“blog/”目录中的 htaccess 文件中使用它。

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,NC,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mydomain.com/blog/$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/blog/$1 [R=301,L]
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /post?title=$1&id=$2 [QSA,L]
4

1 回答 1

0

你的 htaccess 文件很乱。其中很多只会给自己造成额外的混乱。重新开始。

/.htaccess

如果您的根目录中有 htaccess 文件,请确保其中没有与 /blog/.htaccess 中的规则冲突的规则

/blog/.htaccess

Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteBase /

## this makes /blog/whatever-whatever  actually execute /blog/post.php?title=whatever-whatever but only if the file doesn't physically exist
RewriteCond %{DOCUMENT_ROOT}/blog/$1 !-f
RewriteRule ^blog/(.*)$     /blog/post\.php?title=$1   [QSA,L]

/blog/index.php

<?php
$post_title = 'Whatever Whatever';
$post_slug = strtolower(str_replace(' ', '-', $post_title)); // replace spaces with hyphens and lower case everything
echo '<a href="/blog/'.$post_slug.'">'.$post_title.'</a>';
?>

/blog/post.php

<?php
$post_slug = $_GET['title'];

// connect to db.
// construct an SQL query such that your WHERE will have a comparison against your field but that has spaces replaced with hyphens
// for example in mysql it would be something like this
$sql = "SELECT * FROM `your_post_table` WHERE REPLACE(`your_post_title_field_column`, ' ', '--') = '".mysqli_escape_string($post_slug)."'";
// (which by the way is very crude and poses some security risks)
// learn about sql injection at http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php

// then you execute your sql query get your result and proceed as needed...
if (!empty($result)) {
    echo '<h1>'.$result['post_title'].'</h1>';
}
于 2012-10-25T18:37:52.430 回答