Search code examples
wordpress.htaccessmod-rewrite

Redirect Wordpress-built site via .htaccess while excluding root url, wp-admin dir, and wp-login.php


I need to redirect any bridges.edu URLs to losangeles.bridges.edu EXCEPT for the www.bridges.edu itself, the wp-admin directory, and wp-login.php. The first objective (site redirect save for www.bridges.edu) works fine with following coding:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?bridges\.edu$ [NC]
RewriteRule ^.+$ http://losangeles.bridges.edu%{REQUEST_URI} [L,R=301]

However, when I try to exclude the wordpress directories and login, I either get a 500 error using something like the following:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*)?wp-login\.php(.*)$ [L]
RewriteCond %{REQUEST_URI} ^(.*)?wp-admin$ [L]
RewriteCond %{HTTP_HOST} ^(www\.)?bridges\.edu$ [NC]
RewriteRule ^.+$ http://losangeles.bridges.edu%{REQUEST_URI} [L,R=301]

Or I essentially get page not found/server unavailable error as it tries to resolve to non-existent /wp-admin or wp-login.php locations on losangeles.bridges.edu using something like this:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^(.*)?wp-login\.php(.*)$
RewriteCond %{REQUEST_URI} !^(.*)?wp-admin$
RewriteCond %{HTTP_HOST} ^(www\.)?bridges\.edu$ [NC]
RewriteRule ^.+$ http://losangeles.bridges.edu%{REQUEST_URI} [L,R=301]

I've tried several other iterations of the preceding based on a bunch of posts I've found here and on other sites, but I can't quite get it to work.

And apologies if I'm missing something simple; I'm an editor with a decent amount of coding knowledge, but I'm certainly not a developer! (Also, my first post here -- sorry if I've misformatted something. I was sure to look through as many similar posts as possible before creating this one.)


Solution

  • You can use the following :

    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^(www\.)?bridges\.edu$ [NC]
    RewriteCond %{REQUEST_URI} !wp-admin [NC]
    RewriteCond %{REQUEST_URI} !wp-login\.php [NC]
    RewriteRule ^.+$ http://losangeles.bridges.edu%{REQUEST_URI} [L,R=301]
    

    Make sure to clear your browser caches before checking this redirect.