【问题标题】:Apache .htaccess mod_rewrite not working to create "Pretty URLs"Apache .htaccess mod_rewrite 无法创建“漂亮的 URL”
【发布时间】:2015-12-20 06:04:08
【问题描述】:

我试图让用户看到的 URL 看起来“漂亮”。

(例如),制作网址:

http://localhost.com/search/?query=multiple+words+seperated+by+pluses

看起来像:

http://localhost.com/search/multiple+words+seperated+by+pluses

.htaccess 文件中处理重写的部分如下所示:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^search/(.*)$ /search/?query=$1
</IfModule>

为了找出问题所在,我测试了一些东西:

  1. 我去了Rewrite Rule Tester 看看我的规则是否适用于所需的收入。生成的 URL 和预期的一样,所以我断定问题是由服务器引起的。

  2. 接下来,我将RewriteRule, "^search/(.*)$" 的第一部分替换为"^.*$" 以选择所有URL,并将RewriteRule, "^search/(.*)$" 的第二部分替换为/index.phpRewrite 他们到主页以便测试RewriteRule 是否正常工作。它奏效了,让我更加困惑。

    • 重写规则 ^.*$ /index.php

任何建议将不胜感激。

注意:我知道有些服务器需要将选项 FollowSymLinks 添加到 .htaccess 文件中,但是当我尝试查看我的服务器是否是这种情况时,我的错误日志报告了:

[Sun Dec 20 03:43:53 2015] [warn] [client Option FollowSymLinks not allowed in line __ of /home/sites/localhost.com/.htaccess.

注意:现在,http://localhost.com/search/multiple+words+seperated+by+pluses 正在返回 404(找不到页面)错误。

【问题讨论】:

    标签: php apache .htaccess mod-rewrite url-rewriting


    【解决方案1】:

    /search/.htaccess中有这个规则:

    DirectoryIndex index.php
    RewriteEngine On
    RewriteBase /search/
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+)$ ?query=$1 [L,QSA]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-09
      • 2012-05-28
      • 2010-11-01
      • 2012-07-20
      • 2013-07-28
      • 1970-01-01
      • 1970-01-01
      • 2011-08-11
      相关资源
      最近更新 更多