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 to 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 “” to my clients is just bad for business.

And you have registered ?

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

then use, 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 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\
RewriteRule ^(.*)$$1 [R=permanent,L]
1 Like

Instead of the i put the or instead of
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”

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 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, 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 .?{REQUEST_URI} [R=301,L]


  1. Removed the ^ to allow match of
  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 as it contains explanations and sample code. It’s helped many SitePoint members over the years and should help you,



1 Like

I’ve often wondered if I should escape the second dot in 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 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.



1 Like

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


Thanks! :blush:



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