#30340 closed defect (bug) (invalid)
.htaccess bug
Reported by: | MarjWyatt | Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | 4.0 |
Component: | Rewrite Rules | Keywords: | |
Focuses: | Cc: |
Description (last modified by )
I'm writing some redirects for a client. When I was redirecting to a page/post on the same subfolder instance of WordPress, things went fine. When I added a redirect to a URL away from that subfolder, it did not work.
Here is a redirect to another page on the same subfolder instance:
Redirect 301 /chocolate-covered-strawberries-edible-arrangements/ http://www.someurl.com/blog/chocolate-covered-strawberries-arrangements/
Here is the redirect code to an external URL:
Redirect 302 /about/ http://www.someurl.com/about-us/
While troubleshooting this today, I discovered that the cause was in the code generated by WordPress when I saved permalinks.
Here is the .htaccess generated by WordPress when saving permalinks:
# BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteBase /blog/ RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /blog/index.php [L] </IfModule> # END WordPress
Note the duplicate RewriteRule for index...
I revised the .htaccess file to:
# BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteBase /blog/ RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^index\.php$ - [L] #RewriteRule . /blog/index.php [L] </IfModule> # END WordPress
Lo and behold! My redirect to an external URL worked.
I think this is a bug.
Change History (5)
#5
@
10 years ago
Thanks for the complete explanation of .htaccess rules haykayltduk.
At the time I opened the report, I had also tried:
I will say that this didn't work either:
Redirect 301 chocolate-covered-strawberries-edible-arrangements/ http://www.someurl.com/blog/chocolate-covered-strawberries-arrangements/
My pal who is an SEO told me it was a flaw in generated .htaccess code.
In between opening the ticket and your response, I did more digging into the proper method for redirecting on WordPress and came up with this solution:
RedirectMatch 301 ^/blog/chocolate-covered-strawberries-edible-arrangements.*$ http://www.womeurl.com/blog/chocolate-covered-strawberries-arrangements/
Hi MarjWyatt,
Thank you for reporting an issue.
I do not believe that the problem (or fix) are related.
Firstly, there is not a duplicate in the generated .htaccess file.
Going through the rules, the main thing to keep in mind is that [L] indicates the *L*ast part of the rule.
So, the first rule is:
This means if the requested file is index.php, then this is the last rule (and evaluate no further rules) and since nothing is specified (the '-'), then nothing is done.
The second rule is:
In this rule, it says:
Back to your redirect rules.
The problem is your first redirect 301.
If we re-examine the rules above, we'll see that you will hit the second rules, and then go into a loop.
The reason is that your want to redirect to /blog/..../index.php instead.
HTH,
Anand