Forum Moderators: phranque

Message Too Old, No Replies

Awkward redirect and rewrite

Persisting a url through redirects and subfolders

         

Clere

4:23 pm on Feb 22, 2011 (gmt 0)

10+ Year Member



Hi all,

I'm new here. I have had a look around and have not found anything that could answer the question - not to say that it hasn't been covered yet, I might not have looked for the right thing.

The issue comes with the original host switching off their server and our server only has htaccess facilities (shared server) so we cannot access any .conf files.

Our url example.uk.com is held by a third party company and they point it to the server we specify. This was fine before as the company hosting the site sorted out all of the server stuff after the redirect.

We now need to do it ourselves and while we build our own sites, we don't actually do servers, so the world of htaccess is smoke and mirrors to us - we dabble. Our server, http://example.co.uk is the main root and we house sites in sub-domains therein and urls we hold redirect to subfolders also. This has worked a treat until now.

The problem is that we need to remove the first bit of the url, the example.co.uk bit and have the example.uk.com as the bit immediately after http:// Currently it works to http://example.co.uk/example.uk.com/, a simple thing possibly, but we have no experience of this.

Any pointers would be really appreciated.

Thanks

Stuart

g1smd

5:18 pm on Feb 22, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You want to redirect
http://example.co.uk/example.uk.com/<anything> 
to
http://example.uk.com/<anything>
here? Is that what you are asking?

It looks like you also need a rewrite so that requests for
http://example.uk.com/<anything>
are internally rewritten to point to the internal server path at
/var/www/example.uk.com/<anything>
(or similar) too. Is that correct?

Clere

8:58 am on Feb 23, 2011 (gmt 0)

10+ Year Member



In a nutshell, yes. mod_rewrite fills me with trepidation and I would have no idea where to start with this. It was the same with php but I got over that.

g1smd

9:34 am on Feb 23, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



# REDIRECT co.uk+folder requests for index page to correct uk.com domain with slash
RewriteCond %{HTTP_HOST} ^(www\.)?example\.co\.uk$
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.(php|html?)\ HTTP/
RewriteRule ^example\.uk\.com/(([^/]+/)*)index\.(php|html?)$ http://www.example.uk.com/$1 [R=301,L]


# REDIRECT co.uk+folder requests to correct uk.com domain
RewriteCond %{HTTP_HOST} ^(www\.)?example\.co\.uk$
RewriteRule ^example\.uk\.com/(.*)$ http://www.example.uk.com/$1 [R=301,L]




# [optional] REDIRECT co.uk requests for index to / (and fix www at same time)
RewriteCond %{HTTP_HOST} ^example\.co\.uk
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.(php|html?)\ HTTP/
RewriteRule ^(([^/]+/)*)index\.(php|html?)$ http://www.example.co.uk/$1 [R=301,L]


# [optional] REDIRECT co.uk requests for non-www to www
RewriteCond %{HTTP_HOST} ^example\co\.uk
RewriteRule (.*) http://www.example.co.uk/$1 [R=301,L]




# If the server root for www.example.uk.com points to folder
# /var/www/example.co.uk/example.uk.com in httpd.conf
# then move the next two blocks of code to the
# /var/www/example.co.uk/example.uk.com folder


# REDIRECT uk.com requests for index to / (and fix www at same time)
RewriteCond %{HTTP_HOST} ^(www\.)?example\.uk\.com
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.(php|html?)\ HTTP/
RewriteRule ^(([^/]+/)*)index\.(php|html?)$ http://www.example.uk.com/$1 [R=301,L]


# REDIRECT uk.com requests for non-www to www
RewriteCond %{HTTP_HOST} ^example\.uk\.com
RewriteRule (.*) http://www.example.uk.com/$1 [R=301,L]




# Set the server root for www.example.uk.com to point to folder /var/www/example.uk.com in httpd.conf
# OR use the rewrite code below

# Internally REWRITE direct uk.com requests to internal folder 
RewriteCond %{HTTP_HOST} ^www\.example\.uk\.com$
RewriteCond $1 !^example\.uk\.com$
RewriteRule (.*) /example.uk.com/$1 [L]



Code is untested. Report any errors or bugs. Typos are likely.

[edited by: tedster at 8:53 pm (utc) on Feb 23, 2011]
[edit reason] fix typo [/edit]

Clere

10:04 am on Feb 23, 2011 (gmt 0)

10+ Year Member



Wow!

Thanks for that, I think I get where it's going. Am I right in thinking that the second line [THE_REQUEST] tells the server to redirect/rewrite to http://example.uk.com if a root index request is made?

g1smd

10:13 am on Feb 23, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



HTTP_HOST matches the part after the colon of the literal
"
Host: www.example.com
" request sent by the browser.

THE_REQUEST matches the literal
"
GET /somepath/somefile HTTP/1.1
" request sent by the browser.

Use the "Live HTTP Headers" extension for Firefox to see these details.

Be absolutely clear about the differences between "URLs used out on the web" and "server filepaths used inside the server". They are two different things, merely related by the action of the server itself.

Crucial too, is the difference between an external redirect and an internal rewrite. They both use similar looking RewriteRule code but the outcome is very different.

Mod_Rewrite is NOT about "making things on the server appear at specific URLs". That thinking is exactly backwards. It is instead, all about accepting incoming URL requests, examining them, and then either redirecting the browser (using a redirect) to make a new request for a different URL or else mapping the request (using a rewrite) to a specific place inside the server filesystem in order to serve the content.

[edited by: jdMorgan at 7:07 pm (utc) on Mar 4, 2011]
[edit reason] speling in varname. [/edit]

Clere

11:18 am on Feb 23, 2011 (gmt 0)

10+ Year Member



Unfortunately, the server that the boss bought is shared and we don't have access to the httpd.conf file. We can edit .htaccess as much as we like though.

The propagation hasn't completed yet, and the required url is still pointed to the old server (as far as we can see), so any tests are returning nothing at the moment for us.

Clere

2:12 pm on Feb 23, 2011 (gmt 0)

10+ Year Member



Right, the propagation has finally completed and I can now view the site as normal - I have also been told no to worry about the rewrite as it's unimportant. Ummm.

We will have a play in a while properly, but the script for now creates a 500 error. I'm going to look thoroughly at it and work out what it does in minutii.

Thanks for all your help.

g1smd

2:18 pm on Feb 23, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If there is a 500 error, check the server ERROR log to see exactly what it says.

It should explain exactly what the problem is.

Viewing your HTTP session using the "Live HTTP Headers" extension for Firefox is also a very good idea.

g1smd

10:09 pm on Feb 23, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



TYPO.

RewriteCond $1 !^example\.uk\.com$


should be

RewriteCond $1 !^example\.uk\.com


With the typo in place, there would likely be an infinite loop leading to a 500 error.

jdMorgan

7:10 pm on Mar 4, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Might also be that Options +FollowSymLinks and RewriteEngine on are not present...

Jim