Mobile app version of vmapp.org
Login or Join
Eichhorn148

: .htaccess problems with redirection 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

@Eichhorn148

Posted in: #Htaccess #ModRewrite #Php #Redirects

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: 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 site/version, give them site/version.php, else: redirect everything to newsite/
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 ^(.*)$ newsite.com/ [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.

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Eichhorn148

1 Comments

Sorted by latest first Latest Oldest Best

 

@Ogunnowo487

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 (.*) newsite.com/ [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.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme