htaccess的301重定向不工作重定向、工作、htaccess

2023-09-02 00:30:52 作者:欠℃аǒ旳青春

我想要一个简单的301规则添加到codeigniter网站的.htaccess文件。

I'm trying to add a simple 301 rule to the .htaccess file of a codeigniter site.

redirect 301 /newsletter http://sub.domain.com/newsletters/may2010

当我访问 http://sub.domain.com/newsletter 重定向进入

http://sub.domain.com/newsletters/may2010/?/newsletter

我不知道在哪里了?/通讯是从哪里来的。完整.htaccess文件:

I'm not sure where the ?/newsletter is coming from. Full .htaccess file:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /    

    redirect 301 /newsletter http://sub.domain.com/newsletters/may2010

    #Removes access to the system folder by users.
    #Additionally this will allow you to create a System.php controller,
    #previously this would not have been possible.
    #'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #Checks to see if the user is attempting to access a valid file,
    #such as an image or css document, if this isn't true it sends the
    #request to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>



<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.
    # Submitted by: ElliotHaughin

    ErrorDocument 404 /index.php
</IfModule>

#####################################################
# CONFIGURE media caching
#
Header unset ETag
FileETag None
<FilesMatch "(?i)^.*.(ico|flv|jpg|jpeg|png|gif|js|css)$">
Header unset Last-Modified
Header set Expires "Fri, 21 Dec 2012 00:00:00 GMT"
Header set Cache-Control "public, no-transform"
</FilesMatch>
#
#####################################################

<IfModule mod_deflate.c>
<FilesMatch ".(js|css)$">
SetOutputFilter DEFLATE
</FilesMatch>
</IfModule>

我该如何解决这个问题?

How can I fix this?

推荐答案

嘿,这是Apache的拉动订单处理的卑鄙伎俩你。事实证明,你把你的重写命令在文件顶部的事实并不意味着它是什么实际执行的第一个。会发生什么,而不是是的mod_rewrite 首先运行,其次是 mod_alias中(它负责处理你的重写)。

Heh, this is Apache pulling a sneaky order-of-processing trick on you. As it turns out, the fact that you put your Rewrite command at the top of the file doesn't mean that it's what's actually executed first. What happens instead is that mod_rewrite is run first, followed by mod_alias (which is responsible for handling your Rewrite).

这将导致以下改造,每的mod_rewrite

This results in the following transformation, per mod_rewrite:

newsletter --> index.php?/newsletter

的mod_rewrite 开心地设置查询字符串?/通讯,而是因为你没有 PT 指定的标志,做的没有的直通重写URL 的index.php mod_alias中。因此, mod_alias中仍然看到 / newsleter 路径,它重定向到的http://子.domain.com /通讯/ may2010 ,并添加(现改为)查询字符串?/通讯到底帮你搞定。有趣的东西,对吧?

mod_rewrite happily sets the query string to ?/newsletter, but because you don't have the PT flag specified, does not passthrough the rewritten URL index.php to mod_alias. Therefore, mod_alias still sees the /newsleter path, redirects it to http://sub.domain.com/newsletters/may2010, and appends the (now changed) query string ?/newsletter to the end to seal the deal. Fun stuff, right?

总之,一个速战速决的这种情况是无视只是通讯请求:

Anyway, a quick-fix for this scenario would be to ignore requests for just newsletter:

#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !newsletter$
RewriteRule ^(.*)$ index.php?/$1 [L]