mod_rewrite code work

Trying to redirect all instances of


server.hostingcompany.com/~myhostingusername/*

to


actualdomain.com/*

where * is a wildcard


RewriteCond %{HTTP_HOST} !^server\\.hostingcompany\\.com
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]

I half expected the above to work but actually redirect to


actualdomain.com/~myhostingusername/*

But it doesn’t work at all.

Am I missing something? Ta.


RewriteCond %{HTTP_HOST} !^server\\.hostingcompany\\.com
RewriteRule (.*) http://www.actualdomain.com/$1 [R=301,L]

Should
be the above of course - sorry, typo.

Where is this rewrite? It needs to be on the computer serving
server.hostingcompany.com

since this is where the initial request is. That server can then say, no, go to this other server (actualdomain.com). However, actualdomain.com cannot do the redirect.

DD,

Stommes poes is correct about the location of the .htaccess (and you’ll NOT be allowed to put your .htaccess on your host’s DocumentRoot!). HOWEVER, you can put your code within your own DocumentRoot. Where you have gone wrong with your code is:

  1. You’re saying NOT (start {HTTP_HOST} string, server, dot, hostingcompany, dot, com and end of string. Unfortunately, three MAJOR errors here the first of which is the NOT and the second is that you’re requiring the www of the hosting company NOT to be www (instead, you’re requiring ‘server’) and the third is that you’re requiring the entire string to be lowercase.

In the RewriteRule, you don’t need to REcapture the {REQUEST_URI} string so

RewriteCond %{HTTP_HOST} hostingcompany\\.com [NC]
RewriteRule .? http://www.actualdomain.com%{REQUEST_URI} [R=301,L]

… will do EXACTLY what you want.

Regards,

DK

Thanks both.

Sorry, should have noticed the ! for NOT.
On requiring server rather than www, that’s actually what I want to do - the temporary domain given by my hosting company is server.hostingcompany.com/~myusername/ or is that related to not having access to server.hostingcompany.com’s root?
Noted on lowercase - everything should be, but it’s not a requirement.

So testing the code works for redirecting


server.hostingcompany.com/~myhostingusername/

to


http://www.actualdomain.com

But when trying:


server.hostingcompany.com/~myhostingusername/my/path/

it always redirects to


http://www.actualdomain.com/index.php

:-/

Maybe this will work:


RewriteCond %{HTTP_HOST} hostingcompany\\.com [NC]
RewriteRule ~myhostingusername/?(.*) http://www.actualdomain.com/$1 [R=301,L]

That would strip off the ~myhostingusername part from the original URL, so that the mapping should be correct.

Similar:


RewriteCond %{HTTP_HOST} hostingcompany\\.com [NC]
RewriteRule (.*)? http://www.actualdomain.com/$1%{REQUEST_URI} [R=301,L]

Jack,

Naw, Scallio had it correct as your “site” under hostingcompany.com was ~myhostingusername, therefore, you needed to STRIP that (and any trailing /) off the front of the URI and redirect to your domain with the remainder of the URI. Note, Scallio’s optional / after the ~myhostingusername was in case your pseudodomain was called without specifying that it’s a directory (like http://www.actualdomain.com without the trailing /). :tup: to ScallioXTX for correcting my code.

Regards,

DK

Thanks to all three of you for your efforts so far.

However now no redirection is happening at all :frowning:

Doublechecked for typos - not seeing anything awry.

Could you post the code you have so far?
Just post the complete .htaccess if you will, maybe some other rules are interfering with what you want.

DD,

Be SURE to use the [R=301,L] set of flags after your RewriteRule - at least in testing! That will cause the redirection to be visible in the location box and that’s a great tip for EVERYONE! (If the redirection is there as it’s supposed to be, then your script is the problem - otherwise, post your code AND your test URI so we can check the regex et al.)

Regards,

DK

Sure, thanks see below. Will try and see if I can come up with a test URL - don’t want this particular URL in public at the moment.


#
#  mod_rewrite in use
#

RewriteEngine On

# RewriteBase /cms

#
#  Rules
#

# Rule for CMS URL's to rewrite

RewriteRule ^content(.*) index.php
RewriteRule ^component/(.*) index.php

# Rule to redirect index.html to / with 301 permanent

Options +FollowSymLinks
RewriteCond %{THE_REQUEST} ^.*/index.html
RewriteRule ^(.*)index.html$ http://www.actualdomain.com/$1 [R=301,L]

# Rule from sitepoint thread id 672151

RewriteCond %{HTTP_HOST} hostingcompany\\.net [NC]
RewriteRule ~myhostingusername/?(.*) http://www.actualdomain.com/$1 [R=301,L]

order allow,deny
deny from 80.178.190.68
deny from 80.178.167.71
deny from 80.178.195.236
deny from 80.178.148.171
deny from 80.178.211.96
deny from 80.178
deny from 82.80.136.48
deny from 192.117.111.68
deny from 87.248.161.198
allow from all

The .htaccess seems fine.

Maybe your host is running Apache 1.x, so the rule should be:


RewriteCond %{HTTP_HOST} hostingcompany\\.net [NC]
RewriteRule [COLOR="Red"]/[/COLOR]~myhostingusername/?(.*) http://www.actualdomain.com/$1 [R=301,L]

To be sure you can also use


RewriteCond %{HTTP_HOST} hostingcompany\\.net [NC]
RewriteRule [COLOR="Red"]/?[/COLOR]~myhostingusername/?(.*) http://www.actualdomain.com/$1 [R=301,L]

That works on both Apache 1.x and Apache 2.x

PS. The RewriteCond in


RewriteCond %{THE_REQUEST} ^.*/index.html
RewriteRule ^(.*)index.html$ http://www.actualdomain.com/$1 [R=301,L]

seems superfluous to me, if you remove it the RewriteRule alone (without the RewriteCond) should do the job just fine.

Nope - no dice, still nothing - both the / and the /content/view/45/67/ don’t work, but the earlier one does redirect the ~hostingusername/ to /index.php.

DD,

Oh, my! Let me comment your code:

In other words, LOTZA problems in your code!

See, LOTZA stuff to correct :goof:

Wait a minute! /content/view/45/67/ is your URI? That will NOT match ~myhostingusername and the (.*) won’t pickup the view, 45 or 67 as individual pieces of the path/filename as good regex would.

May I recommend that you drop back to “Specificity” and write in words what you want your mod_rewrite code to do providing the test URL, required URL (because you’re changing domain names in the URL - otherwise, URIs are fine) and note what your index.php is expecting (a series of variables in the query string OR just being passed the {REQUEST_FILENAME} so it can parse that rather than a query string. Note that you’ve “defined” the characters and the pseudo path above with content, view, 45 and 67 so do add those!

Regards,

DK

So thanks again for your continued tenacity on this. It does appear that perhaps I did not clearly state my intentions or the goal got muddled somewhere along the way.


RewriteRule ^content(.*) index.php

Where is the LAST flag OR do you mean to AND this with the mutually exclusive start with component/?

RewriteRule ^component/(.*) index.php

Ditto. Just like ANY programming language, you need to terminate your (block) statements!

These lines are part of the .htaccess file supplied with the CMS (Mambo/Joomla). I don’t doubt that they have inefficiencies or bad practice, but I did not write them nor did I write any of this code really :slight_smile: I wish I understood more than the very fundamentals (though I am questioning that statement given your reactions to the code).

As far as I can tell they allow URI’s like this


/content/view/379/30/

to be processed like this:


/index.php?option=com_content&task=view&id=379&Itemid=30

On a vanilla install with nothing else in my .htaccess, that works fine and is how the site has been running and linked to for years. For the avoidance of domain these only need to do the work on actualdomain.com.


Options +FollowSymLinks

This is a little late, isn't it?

Very probably, but it was not included in the original .htaccess but was used in the snippet I obtained from elsewhere but it worked and did not interfere with the CMS authored code.

So starting with the baseline functionality above:

I want to ensure that when someone goes to:

http://www.actualdomain.com/index.php

that it redirects to

http://www.actualdomain.com/

with a 301 redirect.

In reference to the code it would be a good idea to ensure that only URL’s prefixed by the actualdomain.com instead of the hack used.

At this point in my site’s lifetime I decided to move hosts, which I did successfully, however, the test URL (server.hostingcompany.com/~myhostingusername/) given to me prior to the domain propagating over, got into the wild somehow (my fault) and ended up with links to that URL with valid URI’s:

http://server.hostingcompany.com/~myhostingusername/content/view/379/30/#

At this point I decided it would be best to ensure any URL requested beginning

http://server.hostingcompany.com/~myhostingusername/

had any superfluous suffix stripped, and re appended to

http://www.actualdomain.com/

Sorry if that was not clear from the first post with the * as wildcards, but hopefully the full context of why clears it up.

DD

If I were you I would just put this .htaccess on server.hostingcompany.com/~myhostingusername/ :


RewriteEngine On
RewriteRule /?~myhostingusername/?(.*) http://www.actualdomain.com/$1 [R=301,L]

Nothing more, nothing less.

And then let actualdomain.com worry about rewriting /content.* to /index.php, etc.
If you start rewriting at one host, then 301 to another host, and rewrite some more, things are bound to get messy!

DD,

Ah, that’s much better! Thanks! It’s also VERY close to the example I develop in the tutorial Article linked in my signature so, as it’s quarter to one in the AM, my brain is like Jello so I’ll let you read the article and use its example for another go at the mod_rewrite code. I’ll be back in the AM (ha - LATER in the AM) to see what you’ve come up with and lead you to success (my success will be you - and any others who read this thread - learning about what your code is doing).

G’nite!

DK

Thanks - the problem (I think) with that is that both locations are the same place - the htaccess that serves server.hostingcompany.com/~myhostingusername/ is the same as the one that serves: actualdomain.com.

However, with all the above in mind, perhaps this would be a plan:



RewriteEngine On

# First rule to catch temporary hosting URL, 301 redirect, and stop processing.
RewriteRule /?~myhostingusername/?(.*) http://www.actualdomain.com/$1 [R=301,L]

# Next, knowing the next URL won't be one like the above if we are still prcoessing, let the CMS do it's thing
RewriteRule ^content(.*) index.php
RewriteRule ^component/(.*) index.php

# Finally do the index.html redirect to /


Agreed - I will check it out while you get some sleep! :slight_smile: Thanks.