Change url in address bar via htaccess

I’m green @ php or writing htaccess files but i do have some experience with html css and javascript

My problem is that i need to change http://server####.hosting.com/ to http://mydomain.com/ and i’ve figured the way to do it is with htaccess file. I dont know how to use htaccess though. I need that change to be performed in the address bar because showing “server.hosting.com” to my clients is just bad for business.

And you have registered http://mydomain.com ?

I own mydomain.com and the server?
EDIT: Of course mydomain.com is just an example. In reality it’s different.

then use example.com, that’s registered esp. for this case.

but anyways, the only thing that might work is sending a HTTP 301, given that your code is hosted on example.com. otherwise (changing the address, but not the host) this would be called address spoofing.

1 Like

You can redirect from one url to another using something like this in your .htaccess file

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.co.uk
RewriteRule ^(.*)$ http://www.example.com/$1 [R=permanent,L]
1 Like

Instead of the example.co.uk i put the server00000.hosting.com? or instead of http://www.example.com/?
Beacuse i dont know where to put the hosting and where my domain. As i said i am 100% green at this

edit: the way i put it it doesnt work

Could it be that wordpress is managing the site?

edit: it does not work both ways. Am I doing something wrong?

Ah, WordPress.

You want “Domain Mapping”
https://en.support.wordpress.com/map-existing-domain/

It’s difficult to say without seeing what you have, and yes it is possible that the WordPress setup is affecting something.

In the example I gave the .co.uk domain is being redirected to the .com domain. In your case you need to redirect your domain to the hosting dot com domain.

Did you remember the backslash before the dot in the rewrite condition?

vakken,

hosting.com is probably not allowing you to use their .htaccess for your redirection. Have you tested that it works? If so, this modification of gandalf458’s code will do the trick:

RewriteEngine on RewriteCond %{HTTP_HOST} example\.co\.uk [NC] RewriteRule .? http://www.example.com%{REQUEST_URI} [R=301,L]

Modifications:

  1. Removed the ^ to allow match of www.example.co.uk
  2. Added No Case flag ( {HTTP_HOST} is not case sensitive)
  3. Removed the (.*) (:fire: EVERYTHING :fire: atom) and forced match
  4. Replaced $1 with the Apache variable {REQUEST_URI} - no need to capture it again in # 3
  5. Replaced permanent with 301 - same thing but the status code is shorter

You might benefit from reading my mod_rewrite tutorial at http://dk.co.nz/seo as it contains explanations and sample code. It’s helped many SitePoint members over the years and should help you,

Regards,

DK

1 Like

I’ve often wondered if I should escape the second dot in .co.uk but it worked without so I didn’t :rolling_eyes:

Hi gandalf458!

It’s been a while but I thought I’d try to help out a bit (again).

The dot character in a regular expression is a metacharacter which specifies ANY character (which includes the dot character). Of course it worked but it does not specify .co.uk any more than it specifies .coXuk or …

Anyway, I hope my “list of modifications” helped as I tried to briefly explain my suggested/recommended changes.

Regards,

DK

1 Like

Good to have you around again DK. Thanks for the corrections and explanations.

G

Thanks! :blush:

Regards,

DK

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.