2

I'm trying to set up a number of 301 redirects using the .htaccess file. For some reason it is not working for me.

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress
# BEGIN 301 Redirects
Redirect 301 /about-us/ http://www.newsite.com/new-page/about-us/
# END 301 Redirects

Can anyone shed light on why this may not be working for my Wordpress site?

2
  • Please state the URL(s) you want to redirect. Are these URLs also valid pages in your local WordPress site?
    – MrWhite
    Commented Apr 12, 2018 at 0:23
  • And what do you mean by "not working" exactly?
    – MrWhite
    Commented Apr 12, 2018 at 8:54

3 Answers 3

2

In general it is much better to handle redirect on the PHP side. (assuming is is implemented by semi competent developer) It is more flexible and uses less overhead.

Specifically to the question, the set of wordpress rules will handle all URLs, therefor your rule is too late and you should move it to be before the wordpress rules.

8
  • "and uses less overhead." - Please explain. Using PHP will only increase overhead!
    – MrWhite
    Commented Apr 12, 2018 at 8:39
  • htaccess is loaded on each request. in general for each page requests there are probably 30 static content requests. In this case the rewrite logic will be evaluated for all of them although it might apply to only 3% of them Commented Apr 12, 2018 at 8:57
  • a competent wordpress developer will do the redirect check only after all wordpress rewrite rules were evaluated and no match was found, or in other words before displaying a 404 page. This way the check will be done only when there is a good case to suspect that the URL needs a redirect Commented Apr 12, 2018 at 9:00
  • .. obviously can simply set a page in that url and set for it a template that simply redirects. The point is that the logic is in the application and if you move it to nginx you do not need to change it, and since you do the check only when you have good suspision, and not just always your performance will also be better (assuming the url is not being actively used anymore in any links you publish) Commented Apr 12, 2018 at 9:03
  • a different improvement in the htaccess itself can be to move the redirect check to just before "rewriting" to index.php. This way you avoid the check for static files, but still the htaccess itself takes more time to process, and it is still done for all your wordpress pages Commented Apr 12, 2018 at 9:06
0

Try to use following code:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteRule ^old-page-name(/.*)?$ https://www.myurl.com/new-page-name/ [L,R=301,NC]

RewriteRule ^another-old-page(/.*)?$ https://www.myurl.com/another-new-page/ [L,R=301,NC]

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
2
  • 1
    Please, explain how that code answers the question.
    – cybmeta
    Commented Apr 12, 2018 at 7:32
  • 1
    These directives shouldn't be written directly inside the # BEGIN WordPress block. Why do you have (/.*)? on the end of the RewriteRule pattern?
    – MrWhite
    Commented Apr 12, 2018 at 8:58
0

For redirects like these, I would recommend using this plugin: https://wordpress.org/plugins/redirection/ It's easy to use and support even advanced REGEX redirects.

I personally used the .htaccess file just for more advanced and domain related redirects, i.e. domain canonicalization.

But if you really want to do this redirect in .htaccess file, then here is how you need to do it:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# BEGIN 301 Redirects
Redirect 301 /about-us/ http://www.newsite.com/new-page/about-us/
# END 301 Redirects
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.