简体   繁体   中英

Creating links relative to localhost

I'm trying to create a custom menu in drupal 7 by coding it in a custom code block, but I'm running into the issue that the links I create aren't lined up to the way my local site is setup. Is there php code or a drupal setting of some sort that can create a link relative to the local machine setup?

To give more context: My friend and I are working on creating a drupal site and have individually setup our local files differently (we are sharing the database which is on a remote server). When browsing to the site, his URL shows up: localhost/content/page . Whereas how I have it setup is: localhost/sitename/content/page .

When I create an internal link in the nav menu, I have to create it using a relative path /content/page, otherwise the link wont work on my coworkers localhost. This, in turn, makes it so that the link doesn't work on my locahost.

Is there a way I can create these links relative to the localhost so that it works on both machines? When creating a navigation menu using my Drupal theme this is not an issue, but since the link list I'm creating is custom coded, I can't seem to figure out how to mimic this same functionality.

Any ideas? Thanks!

I would suggest looking into 301 redirects in .htaccess locally. Make sure rewrite engine is on and add something like

RewriteRule ^localhost/content$ /localhost/sitename/content [R=301,L]

You could use a constant for the base URL:

Put this somewhere in your entry file:

define('BASE_URL', 'http://localhost/content/');

And then define your paths as:

$path = BASE_URL.'section/page';

That way you only have to change the BASE_URL definition when moving to another system and all links are updated. For PHP includes you can use the same trick with a base path.

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