【问题标题】:RewriteEngine adding .php to queryRewriteEngine 添加 .php 进行查询
【发布时间】:2014-01-02 12:39:54
【问题描述】:

我正在将查询重写为对 SEO 友好的 URL..
基本上,我正在重写
http://example.com/forums/category?v=Name

http://example.com/forums/category/Name

我的问题是 $_GET['v'] 返回 Name.php 而不是 Name.. 这是我的 .htaccess:

<Files .htaccess> 
order allow,deny 
deny from all 
</Files> 

ErrorDocument 400 /404.htm
ErrorDocument 401 /404.htm
ErrorDocument 403 /404.htm
ErrorDocument 404 /404.htm
ErrorDocument 500 /404.htm

RewriteEngine on 
RewriteRule ^(.*)/$ /$1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(([^/]+/)*[^.]+)$ $1.php [L]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php http://example.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /forums/view\.php\?v=([^&]+)&a=(.+)\ HTTP
RewriteRule ^ /forums/view/%1/%2 [R=301,L]
RewriteRule ^forums/view/([^/]+)/([^/]+)/?$ /forums/view.php?v=$1&a=$2 [L]
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /forums/category\.php\?v=([^&]+)\ HTTP
RewriteRule ^ /forums/category/%1 [R=301,L]
RewriteRule ^forums/category/([^/]+)/?$ /forums/category.php?v=$1 [L]

【问题讨论】:

  • $_GET['v'] 在哪个文件中?
  • http://example.com/forums/category/Computing 返回Computing.php

标签: php apache .htaccess url-rewriting url-redirection


【解决方案1】:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(([^/]+/)*[^.]+)$ $1.php [L]

这两个字符串意味着如果您的文件不存在(http://example.com/forums/category/Name 显然不存在)您将其转换为 http://example.com/forums/category/Name.php 并且规则继续进行 - 所以您将 Name.php 作为 category.php 中的参数而不是的Name

要解决您的问题,只需将这 2 个字符串移到其他规则下方即可。

【讨论】:

    【解决方案2】:

    以下规则在将该部分写入查询字符串的规则之前匹配。

    #First matched rule
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(([^/]+/)*[^.]+)$ $1.php [L]
    
    #Before this rule
    RewriteRule ^forums/category/([^/]+)/?$ /forums/category.php?v=$1 [L]
    

    似乎首先匹配的规则是“if-all-other-cases-fail-do-this”,所以你应该将该规则移到底部。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-10
      • 1970-01-01
      • 1970-01-01
      • 2015-03-02
      相关资源
      最近更新 更多