htaccess 301 redirect not working

前端 未结 1 1906
鱼传尺愫
鱼传尺愫 2020-12-22 08:49

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
         


        
相关标签:
1条回答
  • 2020-12-22 09:24

    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).

    This results in the following transformation, per mod_rewrite:

    newsletter --> index.php?/newsletter
    

    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]
    
    0 讨论(0)
提交回复
热议问题