【问题标题】:why is .htaccess not working on live site?为什么 .htaccess 不能在现场工作?
【发布时间】:2018-12-26 00:24:03
【问题描述】:

我需要将 url 重写为“漂亮”而不是查询:它们需要是 /kw/blah 而不是 kw.php?kw=blah。我已经用在线检查器多次测试了代码,他们说它有效/在语法上是正确的。

我曾经检查过的两个网站是:https://htaccess.madewithlove.be(网址测试是使用 /kw/melbourne-web-design 和 http://www.htaccesscheck.com

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} blueshiftwebservices\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://blueshiftwebservices.com/$1 [R,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteRule ^kw/([^/]*)$ landing.php?kw=$1 [L]
</IfModule>

# END WordPress

我收到错误 404 Page not found only with the pretty url,而不是包含查询的 url。

实时站点肯定是指 .htaccess 文件,所以我不知道该怎么办?

【问题讨论】:

  • 你需要在 apache 中启用 mod_rewrite,你已经这样做了吗?

标签: php .htaccess url url-rewriting


【解决方案1】:

规则 RewriteRule . /index.php [L] 匹配所有 URL。所以你必须在它前面插入规则RewriteRule ^kw/([^/]*)$ landing.php?kw=$1 [L]

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    # redirect to https
    RewriteCond %{HTTP_HOST} blueshiftwebservices\.com [NC]
    RewriteCond %{SERVER_PORT} 80
    RewriteRule ^(.*)$ https://blueshiftwebservices.com/$1 [R,L]

    # are you sure you need it?
    RewriteRule ^index\.php$ - [L]

    # make it "pretty"
    RewriteRule ^kw/([^/]*)$ landing.php?kw=$1 [L]

    # process the rest
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
</IfModule>

【讨论】:

  • 非常感谢!将 RewriteRule 放在你说修复它的地方!
猜你喜欢
  • 1970-01-01
  • 2013-02-26
  • 1970-01-01
  • 1970-01-01
  • 2018-06-13
  • 2017-11-05
  • 2021-10-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多