3

我在使用 .htaccess 文件重定向 url 时遇到问题。这是我的 .htaccess 文件的样子:

Redirect 301 /file-name/example.php http://www.mysite.com/file-name/example-001.php
Redirect 301 /section-name/example.php http://www.my-site.com/section-name/example-002.php

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.mysite.com$ [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)/(.*)$ hqtemplates/articles.php?file_name=$2 [L]
php_value session.use_only_cookies 1
php_value session.use_trans_sid 0

现在的问题是,当我转到 page: 时www.my-site.com/file-name/example.php,不是将我重定向到www.my-site.com/file-name/example-001.php它,而是将我重定向到 www.my-site.com/file-name/example.php?file_name=example-001.php.

出于某种原因,它会将“?file_name=example-001.php”添加到 url。有谁知道为什么会发生这种情况以及如何解决?

4

1 回答 1

3

问题是将mod_alias重定向规则与mod_rewrite. 解决方案是坚持一个。这是纯 mod_rewrite only 方法的解决方案(我只包含了与问题相关的部分):

RewriteEngine on
# emulate specific mod_alias Redirect rules
#
# Flags explanation:
#   [L] = last rule, stop processing further rules
#   [R=301] = 301 Redirect
#
RewriteRule ^file-name/example.php$ http://www.mysite.com/file-name/example-001.php [L,R=301]
RewriteRule ^section-name/example.php$ http://www.my-site.com/section-name/example-002.php [L,R=301]

# handle other rewrite requests
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)/(.*)$ hqtemplates/articles.php?file_name=$2 [L]

至于您的 .htaccess 中发生了什么,这是重写日志:

(2) rewrite 'file-name/example.php' -> \
        'hqtemplates/articles.php?file_name=example.php'
(2) strip document_root prefix: /home/test/hqtemplates/articles.php -> \
         /hqtemplates/articles.php
(1) internal redirect with /hqtemplates/articles.php [INTERNAL REDIRECT]
(1) pass through /home/test/Sites/file-name/example-001.php

这表明:

  1. 应用mod_rewrite“重写规则 ^(.+)/(.*)$ hqtemplates...”
  2. file-name/example.php现在相当于hqtemplates/articles.php?file_name=example.php
  3. 应用mod_alias“重定向 301...”
  4. 显示决赛file-name/example-001.php?file_name=example.php
于 2008-11-12T01:00:31.533 回答