【问题标题】:.htaccess - Querystring without index.php.htaccess - 没有 index.php 的查询字符串
【发布时间】:2012-11-20 20:03:17
【问题描述】:

我希望我的网址看起来像这样:“http://www.example.com/start”, 而不是这样:“http://www.example.com/index.php/start”。 这适用于我的 .htaccess 中的这段代码:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1

在一个页面上,我必须使用 GET-Parameters,url 应该如下所示: "http://www.example.com/artists/picasso", 而不是这样: “http://www.example.com/index.php/artists?artist=picasso” 也可以使用以下代码:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /index.php?/$1
RewriteRule ^artists/(.*)$ /index.php/artists?artist=$1 [L]

现在,这个解决方案的问题是,所有其他 url 现在必须以斜杠结尾。 所以它必须看起来像这样:“http://www.example.com/start/”,
而不是这样:“http://www.example.com/start”

感谢您的帮助!

【问题讨论】:

    标签: .htaccess get seo rewrite query-string


    【解决方案1】:

    尝试将您的规则更改为:

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !artists/
    RewriteRule ^(.*?)/?$ /index.php?/$1
    RewriteRule ^artists/(.*)$ /index.php/artists?artist=$1 [L]
    

    主要是,您的模式末尾有一个/$RewriteRule ^(.*)/$ /index.php?/$1,这意味着它必须以斜线结尾才能匹配模式。将其更改为 (.*?)/?$ 使其成为可选。

    【讨论】:

    • 非常感谢! :) 这行得通。但是“RewriteCond %{REQUEST_URI} !artists/”有什么作用呢?
    • @Andri 没有必要,但这是为了防止它匹配像/artists/这样的请求
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-26
    • 2013-06-29
    • 1970-01-01
    相关资源
    最近更新 更多