I apologize ahead of time that this might be a common question, however I've spent 3-4 hours already on solutions which don't quite fix the problem.

I have a site and it is moving to a different domain, the problem is--the site is paired with software which looks for: http://site/version. Using a simple 301 redirect won't work because it'll get '301 Moved Permanently' instead of the actual data. So I thought.. ok--I made version.php which has:

<? echo file_get_contents('http://newsite/version'); ?>

So the idea is simple: When one requests http://site/version, give them http://site/version.php, else: redirect everything to http://newsite/$1

What I got was something like this:

RewriteEngine On
RewriteBase /
RewriteRule ^/version$ /version.php
RewriteCond %{HTTP_HOST} site\.com$ [NC]
RewriteCond %{REQUEST_URI} !^version$
RewriteRule ^(.*)$ http://newsite.com/$1 [R=301]

I am no master at mod_rewrite rules, this is just what I hacked together and it doesn't work or I wouldn't be here. If someone who knows them better than I can help, I'd be very grateful. Thank you in advance.

  • I assume both site and newsite are hosted at the same place - one is a parked domain on the other? – MrWhite Aug 31 '13 at 22:30
  • that is not the case, they are completely different locations. – u8sand Aug 31 '13 at 23:08
  • In that case (unless there are other hosts/domains involved) then you don't need to check against %{HTTP_HOST}, since it will always be site.com, and these RewriteCond directives can be removed. – MrWhite Aug 31 '13 at 23:56

I think you have the correct idea, but the directory prefix (/) is probably the main thing that is causing problems. Try the following...

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^site\.com$ [NC]
RewriteRule ^version$ /version.php [L]

RewriteCond %{HTTP_HOST} ^site\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/version
RewriteRule (.*) http://newsite.com/$1 [R=301,L]

The RewriteRule pattern in per-directory .htaccess files is matched against the URL less the directory prefix (ie. version, not /version), so your pattern would never match.

Conversely the REQUEST_URI variable always includes the full URI, including the directory prefix (/). And in this case, it is likely to be the rewritten URL, ie. /version.php and not /version. Although I simply removed the end-of-string placeholder ($) so it will catch both anyway - assuming you don't have other URLs that start "/version"?

EDIT: Note that if site.com and newsite.com are completely separate sites (as suggested in comments) and no other domains are parked at site.com then you don't need to check the %{HTTP_HOST} and these RewriteCond directives can be removed.

  • This worked out perfectly. Thank you very much for your help as well as your explanation. I do not have any other url's prefixed with version but if that does happen I can simply do ^version(|.php)$ – u8sand Aug 31 '13 at 23:16
  • Yes, although it is probably preferable to write that as ^/version(\.php)?$ to make the ".php" optional and remember to escape the . in the regex. To be honest, I'm pretty sure you can just replace this with ^/version\.php$ (the rewritten URL), providing your directives remain in the same order as above. – MrWhite Sep 1 '13 at 0:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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