Using .htaccess, can you hide the true URL?

10,038

Solution 1

This is really something that the hosting ISP should re-point on their end:

<VirtualHost *:80>
    ServerName www.mysubsite.com
    DocumentRoot /path/to/projects/mysubsite
</VirtualHost>

You might be able to fix it in .htaccess:

RewriteCond %{HTTP_HOST}  ^www\.mysubsite\.com [OR]
RewriteCond %{HTTP_HOST}  ^mysubsite\.com
RewriteRule ^(.*) /path/to/projects/mysubsite/$1 [L]

Note that I did not include [R] in the flags passed to RewriteRule, as that would tell the webserver to send a redirect back to the web browser. If the two sites are on different servers, you would have to have mod_proxy enabled:

RewriteCond %{HTTP_HOST}  ^www\.mysubsite\.com [OR]
RewriteCond %{HTTP_HOST}  ^mysubsite\.com
RewriteRule ^(.*) http://www.myrootsite.com/projects/mysubsite/$1 [P,L]

Solution 2

You can't hide your real URL because it's can risk many websites and users but you can make redirect page like that:

<?php
$url = $_GET['url'];
header('Location: '.$url);

and you can change the file name with the .htaccess

Share:
10,038

Related videos on Youtube

Richard Muthwill
Author by

Richard Muthwill

Coding enthusiast that loves to write small snippets of code to test and see what can come of it. Worked for a time as a teacher, teaching computer science, web design and programming. Languages: HTML / CSS Javascript / jQuery PHP / MySQL Python C#

Updated on September 18, 2022

Comments

  • Richard Muthwill
    Richard Muthwill over 1 year

    So I have a web hotel with 1 main website

    http://www.myrootsite.com/
    

    and a few websites in subdirectories, in a folder called projects.

    I have domain names pointing to the subdirectories, but when holding the mouse over a link in those websites the URLs are shown as:

    http://www.myrootsite.com/projects/mysubsite/contact.html
    

    When I'm on mysubsite.com I want them to be shown as:

    http://www.mysubsite.com/contact.html
    

    I spoke to support for the web hotel and the guy said try using .htaccess, but I'm not sure exactly how to do this.

    Thank you very much for your time!

    Edit: For more information

    My website is: http://www.example1.com/ and I also own http://www.example2.com/.

    All of example2.com's files are in: example1.com/projects/example2/.

    When you visit example2.com, you'll notice all of the URL's point towards:

    example1.com/projects/example2/
    

    but I want them to point towards:

    example2.com/
    

    Can this be done? I hope this is enough info for you to go on :).

    Edit: For w3d

    I go to the url mysubsite.com and the browser shows the url mysubsite.com. The services I'm using create an iframe around myrootsite.com and use the url mysubsite.com

    I just hate that in Firefox and Internet Explorer, holding the mouse over link show that the destination url is: myrootsite.com/projects/mysubsite/...

    • John Conde
      John Conde about 10 years
      I have to close this for now because there isn't enough information here to help you. Without knowing all of the the potential URLs your site has it isn't possible to offer a solution. In the meantime you can look at a tutorial like this one to attempt to write yourself.
    • Richard Muthwill
      Richard Muthwill about 10 years
      I've added some more info to the question, I hope this en enough to answer :) I've had a look through the tutorial you linked, but I can't find anything in it that could help me with this
    • MrWhite
      MrWhite about 10 years
      Does the URL in the address bar show myrootsite.com or mysubsite.com, when you first visit mysubsite.com? Or are you redirecting? How do you generate your in-page links?
    • Richard Muthwill
      Richard Muthwill about 10 years
      I added some more info for you @w3d! :)
    • Richard Muthwill
      Richard Muthwill about 10 years
      @w3d I don't think my last comment worked because I added a ! at the end..
    • MrWhite
      MrWhite about 10 years
      Unfortunately I don't think this can be done with .htaccess wizardry alone - you do need to actually change all those in-page links to the desired URL structure. However, the fact that these two URLs are on different domains are going to give you more trouble. IMO you need to get rid of the IFRAME and setup mysubsite.com as an add-on domain (in cPanel parlance) to myrootsite.com, pointing directly at www.myrootsite.com/projects/mysubsite/
    • Richard Muthwill
      Richard Muthwill about 10 years
      I made a test where I used jquery and changed all the hrefs from contact.html to www.myrootsite.com/projects/mysubsite which isn't the best solution, but it works.. <br/><br/> They do have that option, but do you know what? They do the exact same bloody thing, they just make an iframe with the long url as the src @w3d