Brett Romero
Brett Romero

Reputation: 353

Mod_rewrite not working for some edge cases

This question is similar to others I have seen on here, but after trying many of them, I have not been able to find a solution that works.

The issue is that when I initially installed wordpress, being a noob, I didn't take the wordpress files out of the wordpress folder, and so all my URLs were in the form example.com/wordpress/some-post-name.

Due to a disgracefully slow hosting service, this week I decided to migrate the whole site to Digital Ocean, and at the same time, fix the URLs to remove 'wordpress'. To make sure I didn't break all the links I've built, I then decided to add a mod_rewrite to make sure my old links keep working. After some research I came up with a rule that appeared to work in most cases:

RewriteRule ^wordpress(.*)$ http://example.com$1 [R=301,NC]

It seemed to work for all my pages and posts, but I couldn't get it to work for a couple of cases:

  1. It doesn't work for example.com/wordpress/, my old home page.
  2. It doesn't work for pages with URL parameters (e.g. example.com/wordpress/?tag=kaggle)

To complicate the matter, I added the R=301 flag which seems to have confused everything by caching the changes (I now realize I should have waited until everything else was finalized), so it's basically become impossible for me to continue testing.

So, I come to you - can anyone spot a reason why the RewriteRule specified wouldn't work on the two examples given? Any other issues with that rule I am missing?

Upvotes: 0

Views: 80

Answers (1)

gargkshitiz
gargkshitiz

Reputation: 2168

Try

RewriteRule ^/wordpress/(.*)$ $1 [R=302,NC,QSA,L]

I have tested it with various URLs and it works.

This rule instructs rewrite engine to omit '/wordpress/' from all incoming links unconditionally. Refer http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritecond if you want to do it on particular condition(s).

301 helps to permanently redirect all clients to the new URLs. Refer https://www.hochmanconsultants.com/301-vs-302-redirect/

QSA helps to retain query string. Refer https://httpd.apache.org/docs/2.4/rewrite/flags.html#flag_qsa

L stops the processing of further rewrite rules (if present). Refer https://httpd.apache.org/docs/2.4/rewrite/flags.html#flag_l

Upvotes: 1

Related Questions