gpt4 book ai didi

.htaccess - .htaccess 中的重定向规则将我带到了错误的地方

转载 作者:行者123 更新时间:2023-12-02 00:46:39 25 4
gpt4 key购买 nike

我在使用 .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

现在的问题是,当我转到页面时: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。有谁知道为什么会发生这种情况以及如何解决?

最佳答案

问题是将 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 "RewriteRule ^(.+)/(.*)$ 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

关于.htaccess - .htaccess 中的重定向规则将我带到了错误的地方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/282541/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com