简体   繁体   中英

How to refer main domain directory from sub domain wed address

Consider my domain is www.example.com

I if use path like /images/sample.jpg in php/html file, it actually refers to www.example.com/images/sample.jpg

Similarly if I do the same for www.subdomain.example.com

/images/sample.jpg --> it refers to www. subdomain .example.com/images/sample.jpg

but i want to refer the image from main domain without giving full web address in sub domain web page.

My main domain files are ketp at : root/www/

My subdomain files are kept in : root/public_html/admin/

I may change the web address later.. so I don't want to hard code the full web address in my code.

Do you just want to use html? If you use JavaScript you could programatically create the subdomain. What's the reason for you not wanting to specify the full web address?

Edit:

One way you could do it is by using javascript.

If you set a variable for the domain at the beginning of the text:

<html>
<head>
<script>
var webaddr = "www.example.com";
</script>
</head>

Then use this code when you want to write an actual link:

<body>
<img src="<script>document.write(webaddr)</script>/images/sample.jpg">
</body>
</html>

Then you can change the domain in the variable when you move the domain. Would that work?

If you can use PHP or ASP you can use a variable (or constant) as a placeholder for the host name you want to use. Set that variable in a global configuration file so that it's accessible everywhere.

Here is a PHP example using a constant:

<?php
define( 'HOST', 'www.example.com' );
?>

Then use it like this:

<img src="<?php echo HOST ;?>/images/sample.jpg">

Then you can change your preferred host name whenever you want and all you have to do is update the constant.

This code will detect the URL and add a specified subdomain:

      <script>
            var subdomain = "subdomain"; //change this variable to match your subdomain

            var webaddr = (document.URL); //This gets the current URL
            var newURL = (webaddr.substring(0, 7) + (subdomain) + "." + (webaddr.substring(7))); //This creates the new URL with the subdomain added
        </script>

    Old link:
        <a href="<script>document.write(webaddr)</script>/images/sample.jpg">"<script>document.write(webaddr)</script>/images/sample.jpg"</a>

        <br>
New link:
        <a href="<script>document.write(newURL)</script>/images/sample.jpg">"<script>document.write(newURL)</script>/images/sample.jpg"</a>

See http://jsfiddle.net/wFMzx/2/

Looks like you are wanting something like what apache mod_rewrite does, which is to redirect one type of url to another

for instance when you provide like

/maindomain/images/someimage.jpg 

which would be seen as

www.subdomain.example.com/maindomain/images/someimage.jpg

have it redirected to

www.example.com/images/someimage.jpg

Micronovae seems to have a mod called mod_rewrite that looks to work like apache mod_rewrite

and then you could use something like below on your www.subdomain.example.com site

RewriteRule ^/maindomain/images/(.*)$ http://www\.example\.com/images/$1 [R=301,L]

The Micronovae mod you have to buy though, you could try using http://www.iis.net/downloads/microsoft/url-rewrite but it may have a different syntax for doing the rewrites

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM