Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
250 views
in Technique[技术] by (71.8m points)

wordpress - 301 Redirect to Incorrect URL

I inherited a live Wordpress website where 30+ pages were 301 redirected to the home page. Which is... obviously not ideal.

This is an example of what is happening:

https://www.example.com/shop -- 301 --> https://example.com

In that scenario, this is what I want to be happening:

https://www.example.com/shop --> https://www.example.com/shop

I'd consider myself a beginner at Apache. I don't typically use htaccess except for basic SSL redirects. I've been banging my head against the wall all night so any advice is welcome.

EDIT:

Caching doesn't seem to be the issue as the redirect persists in Chrome incognito. Here's my current .htaccess:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

UPDATE:

This code in the wp-config file appears to be causing issues. When I comment it out, some of the URLs are no longer 301 but the page content that is loading is still the home page on every page.

define('WP_SITEURL', 'https://' . $_SERVER['HTTP_HOST'] . '/');
define('WP_HOME', 'https://' . $_SERVER['HTTP_HOST'] . '/');
question from:https://stackoverflow.com/questions/65641607/301-redirect-to-incorrect-url

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your file looks very similar to the WordPress default, except for a couple items. First, you have multiple RewriteBase directives. The last one wins and controls the entire .htaccess file. This should be corrected. Second, you have a redirect from http protocol to https, which is probably fine and intentional.

According to the WordPress documentation, the default WordPress .htaccess file contains:

# BEGIN WordPress

RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# END WordPress

Working from the WordPress default file above, your file has some added lines that I'll explain.

  1. Ths is a conditional test if the mod_rewrite module is enabled. It's fine, but may be optional in your case.

     <IfModule mod_rewrite.c>
    
  2. The first two lines below are fine. The second instance of RewriteEngine On should be removed, and may be the source of your issue.

     RewriteEngine On
     RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
     RewriteEngine On
    
  3. These lines check for http protocol and redirect to https.

     RewriteCond %{HTTPS} off
     RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
  4. This is the end of the conditional block. It's fine, and required because you have the line in number 1 above.

     </IfModule>
    

Basically, your file looks correct to me, except for the second RewriteEngine On directive. If you remove that and still have issues, I don't think you have an .htaccess problem and should look for other issues in WordPress such as redirection plugins.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...