使用htaccess创建一个漂亮的URL

时间:2012-07-22 01:38:35

标签: php .htaccess href

我想缩短此网址:'http:// localhost / school / readmore?id = 2'(必须在引号中,因为SO不允许发布localhost网址)。我已使用此.htaccess文件将其设为'http://localhost/school/readmore-2.html':

RewriteEngine On
IndexIgnore *

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f

RewriteRule ^readmore-([1-9]+).html$ readmore.php?id=$1

这是修改后的链接

<a href="readmore-<?php echo $show_row['id']; ?>.html">Readmore</a>

但我希望网址看起来像'http:// localhost / school / readmore / 2'。 一旦我尝试但它完全失败了;这是不起作用的脚本:

RewriteEngine On
IndexIgnore *

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f

RewriteRule ^readmore/([1-9]+).html$ readmore.php?id=$1

<a href="readmore/<?php echo $show_row['id']; ?>.html">Readmore</a>

起初我认为它有效但CSS脚本没有加载,因为我在链接中将“-”更改为此“/”,脚本将其作为文件夹读取。

2 个答案:

答案 0 :(得分:1)

逃离.

RewriteRule ^readmore/([1-9]+)\.html$ readmore.php?id=$1

答案 1 :(得分:0)

您的浏览器认为它位于子目录中,因此它引用了“http://localhost/school/readmore/style.css”而不是“http://localhost/school/style.css”

要解决此问题,您应该使用样式表和链接的绝对路径,例如

<link rel="stylesheet" href="http://localhost/school/style.css" />

<link rel="stylesheet" href="/school/style.css" />
相关问题