繁体   English   中英

htaccess子目录中的重写问题,并使用varibales…已卡住

[英]htaccess rewrite issue within subdirectory and using varibales… stumped

我搜寻了许多尝试过的东西,但都没有用……这超出了我。 我已经尝试了至少50件事,但是没有运气。

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

我正在尝试做的是拥有一个更加整洁和SEO友好的网址...

例如:在“ 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而不是上面的显示! 如果有人可以帮助我删除出现在空格的帖子标题中的“%”并替换为“-”,那也很棒。

:: desperate ::再次感谢亚当

当前在“ 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]

您的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>';
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM