nzifnab
nzifnab

Reputation: 16092

RewriteRule to redirect to subdirectory based on HTTP_REFERER

I have a problem where a wordpress install was moved from / (root) to /beta/ (subdirectory). I have it working great, except that there are some page links that are linking to, for instance, /my-page which is now a 404, and I want it to redirect to /beta/my-page. These links exist on /beta/(.*) paths, so my thought was to make a .htaccess rule in the main root path that would see that a link came from /beta/* and simply redirect the user back to the proper page. This is what I have so far:

RewriteEngine On
# `HTTP_REFERER` contains my beta path...
RewriteCond %{HTTP_REFERER} /march\-beta
# The current page is not *already* a `/march-beta` page (don't want an infinite redirect)
RewriteCond %{REQUEST_URI} !/march\-beta
# Remap the entire path to be in the correct subdirectory
RewriteRule ^(.*)$ /march-beta$1 [R=302, L]

However... I'm getting a 500 server misconfiguration kind of error when I save the file in the webroot of the server.

An example:

If a user is on /march-beta/t-shirts

and clicks on a link that links to: /blue-t-shirts (or even /t-shirts/blue-ones)

I want the .htaccess file to see that the HTTP_REFERER contains /march-beta and immediately R=302 them to /march-beta/blue-t-shirts (or /march-beta/t-shirts/blue-ones, in the second case).

Upvotes: 0

Views: 596

Answers (1)

Jon Lin
Jon Lin

Reputation: 143866

You're getting a 500 server error because you have a space between your rewrite flags. The space makes mod_rewrite think that the flags are [R=302, and that horribly confuses mod_rewrite and throws an error. Try removing the space:

RewriteRule ^(.*)$ /march-beta$1 [R=302,L]

Also, you're probably going to want a slash after /march-beta because the URI handed to rewrite rule's in an htaccess file has the leading slash stripped off:

RewriteRule ^(.*)$ /march-beta/$1 [R=302,L]

Upvotes: 1

Related Questions