mark mark - 2 months ago 9
Apache Configuration Question

mod_rewrite for a WordPress page doesn't seem to work

I have a WordPress site and a custom site and send a user from the WordPress site to the custom site when they navigate to a specific URL. Let’s say the main WP site is located at www.example.com. If someone goes to http://example.com/p/M12345, we want to send it to https://my.example.com/myexample/members/login.html?login=M12345. I thought the best way to do this would be to use mod_rewrite in the .htaccess file

I tried setting this up the main .htaccess file, but nothing seemed to work. The WP site at www.example.com uses a redirect plugin which normally takes care of this, but last week it messed up our entire site, so I am reluctant to continue using it. I would like to use the .htaccess file since this feels like the correct way to do it.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(\/[p\/)([mM]\d{5})$ https://my.example.com/myexample/members/login.html?login=$2 [R=301,L]
</IfModule>


A few things we have tried:


  • adding flags such as [NC,R=301,L], [L,QSA] and others

  • changed RewriteRule to Redirect 301 and removed the flags

  • added RewriteCond ^(/p/)$ thinking this would only occur on the example.com/p/ pages



What is the correct way to get this working?

Answer

Leading slash isn't matched in .htaccess and there is en extra [ before p. Also there is no need to escape /:

RewriteEngine On
RewriteRule ^p/([mM]\d{5})/?$ https://my.example.com/myexample/members/login.html?login=$1 [R=301,L]
Comments