Mod_rewrite麻烦:想要从?=指向扁平链接,似乎没什么用

时间:2011-01-17 22:33:27

标签: php .htaccess mod-rewrite

我的网站目前提供的结果为example.com/index.php?show=foo 我希望它能阅读example.com/show/foo

我的理解是,这会使搜索引擎机器人看到它们,这似乎比创建几百个html文件更简单:

我尝试了以下.htaccess代码:

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^show/(.*)$ index.php?show=$1 [NC,L]

没有骰子。

还试过这个,我在另一个stack overflow question找到了:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9A-Za-z]+)/?$ /index.php?show=$1 [L]
</IfModule>

关于我在这里缺少什么的想法?

2 个答案:

答案 0 :(得分:0)

请注意,^([0-9A-Za-z]+)/?$将尝试匹配请求URI,该请求URI仅包含可选斜杠字符串的字母数字字符。因此,URI show/foo将不匹配,因为末尾有更多字符(即,在斜杠之后,表达式期望找到字符串的结尾)。

尝试:

RewriteRule ^show/([0-9A-Za-z]+)/?$ /index.php?show=$1 [L]

此外,要捕获aditional查询参数,您可以执行以下操作:

RewriteRule ^show/([0-9A-Za-z]+)/?$ /index.php?show=$1&%{QUERY_STRING} [L]

这意味着像/show/page?id=1这样的网址会重写为/index.php?show=page&id=1

答案 1 :(得分:0)

我在使用“seo友好”网址的网站上执行此类操作。

在.htaccess中:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /index.php [L]

然后在index.php上:

if ($_SERVER['REQUEST_URI']=="home") {
    include ("home.php");
}

.htaccess规则告诉它如果找不到要求的文件或目录,则加载index.php。然后你只需要解析请求URI来决定index.php应该做什么。

你走在正确的轨道上,但你的规则有问题。 .*非常具有包容性。从那里开始,如果需要,可以采取更严格的限制措施。