WordPress.org

Make WordPress Core

Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#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 ocean90)

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)

#1 @ocean90
7 years ago

  • Description modified (diff)

#2 @ocean90
7 years ago

  • Component changed from General to Rewrite Rules
  • Focuses administration removed

#3 @haykayltduk
7 years ago

  • Resolution set to invalid
  • Status changed from new to closed

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:

RewriteRule ^index\.php$ - [L]

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:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]

In this rule, it says:

  • if what is requested is not a file AND
  • if what is requested is not a directory THEN
  • rewrite the requested URI to /blog/index.php (and evaluate nothing further)

Back to your redirect rules.

The problem is your first redirect 301.

Redirect 301 /chocolate-covered-strawberries-edible-arrangements/ http://www.someurl.com/blog/chocolate-covered-strawberries-arrangements/

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

#4 @SergeyBiryukov
7 years ago

  • Milestone Awaiting Review deleted

#5 @MarjWyatt
7 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/

Note: See TracTickets for help on using tickets.