Wordpress appending URL in HTML form - php

How can I stop Wordpress from appending a URL and instead go to an absolute one?
By this, I mean I have a form which posts to a php page, and in the action parameter I have
action="www.oursite.com/feedbackpage/"
( I have also tried just feedbackpage/), but Wordpress always just appends it to the current URL (so it would be http://www.oursite.com/pagewithform/www.oursite.com/feedbackpage/)
How do I get wordpress to redirect the user to an absolute URL?

You need to make sure you either hardcode the http:// or set it to a proper relative path (so http://www.oursite.com/feedbackpage or /feedbackpage/).
Without the http:// your link is being treated as relative so it is looking for the 'www.oursite.com' folder in the current folder 'feedbackpage'.

Related

How to link to a php file that is in a folder named php

I have made my first website and I have a simple contact form that runs a php scripts to send the details inputted in the form to my email. I initially had all the files in the root directory but I am trying to tidy it up so I now have,
all html files in the root directory, I then have folders for php, css, assets, js.
I know that to link to these I need to just do something like,
<link rel="stylesheet" href="css/contactus.css"/>
I believe this tells the html to look in the root directory, then in css folder and for the file contactus.css.
This seems to work for all the css files but not for the php file ( I get a 404 error). I know this file is correct as when all the files where in the root folder it worked fine.
I currently have,
<form action="php/contactform.php" method="post">
I believe my issue is that when using href it will automatically start in the root folder but as I am not using href in this case it is not starting in the root folder. So I just need to find out what I use to link to a different folder to that of the html file (If I put the php file back in the root folder it works fine.
DIR structure
The action and href attributes work exactly same. The final url depends on how you've written the url in these parameters and what is your current url.
Let's say the current url is http://localhost/my-project/html/my-form.html
If your url in action looks like php/contactform.php this is called path relative url. To get final url the browser will append relative url after last / in current url resulting in http://localhost/my-project/html/php/contactform.php.
You can use .. in the relative url if you want to reference directory higher in the structure. Url ../php/contactform.php will result in http://localhost/my-project/php/contactform.php
If your url in action looks like this /php/contactform.php it is root relative url. The main difference is the starting /. In this case the browser will put your url right after the domain name resulting in http://localhost/php/conactform.php.
Based on your screenshot both /php/contactform.php and php/contactform.php should work fine because the url of the html file should be http://your-domain.example/DJ_contactme.html so both urls should result in http://your-domain.example/php/contactform.php
If you are using mod_userdir your url probably looks like http://your-domain.example/~dannyjeb/DJ_contactme.html in that case the php/contactform.php should work as it would result in http://your-domain.example/~dannyjeb/php/contactform.php
From DJ_Contactme.html you target css file like this:
<link rel="stylesheet" href="./css/contactus.css"/>

Relative Link to Subfolder?

I have the link: www.mysite.com/jobs/stackoverflow
And I want to get this link using a relative file path: www.mysite.com/jobs/stackoverflow/123
Right now I am getting the current url using PHP and then appending '/123' onto the end of the 'href' value to access the subfolder. This method works but I was wondering if there is a way to write a relative link that will link to a subfolder. At first I thought it would be as easy as typing:
Link
but that replaces the 'stackoverflow' part rather than appending it onto the end.
I'm fine with using the PHP solution and I am not looking for a javascript or jQuery solution, I was just curious to see if there was a simple way to do this using only HTML.
I found someone linking to the current directory like this:
Basic HTML - how to set relative path to current folder?
so putting the ./ before the 123 in your link might work.

wordpress image path changing dependent on file

I'm having problems with the paths to my first wordpress theme. Post images and stuff not related to css is located in wordpress_folder/blog-images/ and in index.php when I link to images I use this path: blog-images/img.jpg
The problem is now that when I want to link to the same image from another file (not index.php) in this case single.php wich displays one blog post, the correct path is now ../../../blog-images/img.jpg
This is causing problems in the includes like sidebar etc. sidebar.php works fine when called from index.php but the images path is changed if sidebar.php is called from single.php.
Does anyone know what's going on?
If you are creating these links from within php scripts, I would suggest using the site_url() function to get the URL for your wordpress install and then appending your images path to the end of that. If you are editing static theme files like css, then you should use /wordpress_folder/blog_images/img.jpg.
Something like <img src="<?php echo site_url() ?>/blog_images/img.jpg" /> should be sufficient from theme files.
The reason that paths are chaning is because if you are in wordpress_folder then the path blog_images/img.jpg resolves to wordpress_folder/blog_images/img.jpg but if you are on a post that has the url yoursite.com/wordpress_folder/2011/09/category/my_great_post then the path would resolve to wordpress_folder/2011/09/category/blog_images/img.jpg which is obviously incorrect.
For this reason you should try to use the absolute path or full URL so that no matter what file/folder/url you are linking from, the path will always be correct.
The main downside you may run into is that if you were to change the name of your wordpress folder, or remove it altogether, then you may need to make a lot of edits to reflect that. But in any case, you should put the / in front of your path so that it can be referenced the same from everywhere.
Also check out the site_url() reference page, it lists some other helpful functions at the bottom that may be useful to you.
I thought this was a little unclear from drew's answer, so I am adding a little bit more in a separate answer. His advice is sound and I agree with him.
If you prepend a url with a / then it will navigate based on your site url. Without the slash it uses relative navigation.
So here are some examples for www.mydomain.com
//always shows the image located at http://www.mydomain.com/myfolder/pic.png
//no matter what the url is
<img src="/myfolder/pic.png" />
//shows the image located relative to the current path
//if current url is http://www.mydomain.com/posts/ then the image will come from
//http://www.mydomain.com/posts/myfolder/pic.png
<img src="myfolder/pic.png" />
If you are creating links dynamically from php side then you will want to use site_url().
If you are creating links to your theme directory folder then you will want to use bloginfo('template_directory')

mod_rewrite changes the current dir

I have a php file that loads an article from a db based on the given variables. There is also an .htacces file in the root of the site. I used this in the htaccess to redirect
RewriteRule
^articles/([a-zA-Z0-9-_\s]+).html$
template/index.php?action=viewarticle&alias=$1
after redirecting, the page shows fine but the html in the page goes wrong, for example:
media/2011/02/21/logos.jpg turns in to articles/media/2011/02/21/logos.jpg
This happens because the htacces is redirecting. Is there anyway to do this redirect while keeping the root dir unchanged?
This happens coz the htacces is redirecting.
No, this happens because the browser thinks that
example.com/articles/my_article.html
is a resource in the /articles sub-directory, and treats all relative URLs as relative to /articles.
There is no way to change that behaviour.
You will need to start using absolute image references, or relative image references that consider the additional directory:
<img src="/media/2011/02/21/logos.jpg"> <------ recommended
<img src="../media/2011/02/21/logos.jpg">
you could also use <base> as suggested by #Boris but absolute paths (or full URLs) are a vastly cleaner solution to the problem in my opinion.
First, what do you mean by "html in the pages goes wrong":
Is it the link showed in the status bar?
Is it the actual href? If it is, you probably use some view helper which construct your "base url"
Maybe you "link" your resource without specifing an absolute path (using /), then your resource are "relatively" linked to current page (/articles/)
.htaccess don't change anything in your code.
There is an html element which allow you to define base url used everywhere in your page.
<base href="/root" />
if you have for example Article 12 then when clicking on the link, you will redirect to /root/articles/12
Also, mixing Pekka's answer with Boris', you should define somewhere in your application which is the root path of your application and output all paths as absolute, prepending the base dir you defined earlier.
for example: in config.inc.php
define("ROOT_URI", "http://myserver.com/myapp");
everywhere:
<img src="<?php echo ROOT_URI;?>/media/2011/02/21/logos.jpg
This is like using the base element as Boris suggested, without using it (I also dislike base), and makes your application able to work in whatever folder under the webserver it is stored.

Best practices with multiple directories in HTML

I haven't found a clear answer to this question (but have determined the HTML tag is more trouble than it's worth.)
When you're working with multiple directories on a website, how do you make sure relative links to the rest of your site work as you change your current directory? I don't want my link to "/index.php" to actually link to "/support/index.php" when I go to the support directory.
We're using PHP, so I could use output buffering to change links, but I want to see if others have any good ideas. Could also implement it through Smarty in one way or another. I haven't built a website from scratch that has used multiple directories simply because I don't know of an easy way to deal with this, but the problem shouldn't be too difficult.
(Running on IIS, but obviously it would be better to let it work on any server.)
you could declare a base_url variable, or declare a constant containing your base url
e.g.
DEFINE('BASE_URL', 'http://example.com/');
when using links
e.g.
Home
You already have everything you need
how do you make sure relative links to the rest of your site work as you change your current directory?
we're using absolute links for that
I don't want my link to "/index.php" to actually link to "/support/index.php" when I go to the support directory.
Lucky you, it will never happen
/index.php is absolute path and will never point anywhere beside /index.php.
/ is not just for decoration. It the meaning of slash at the beginning of the path is "root directory". So, /index.php means index.php placed in the root directory.
/support/index.php means index.php placed in the support directory which is placed in the root
easy-peasy. just always use absolute path (not URL which is senseless)
I store a “base” URI in two locations: (i) on the PHP/Zend Framework server, my configuration.xml file holds conventional values such as URIs; (ii) on the client side a more shallow, hidden <form/> holds other (less security compromising) values such as a base URI.
The form, by the way, looks something like this:
<form id="AppSettings" action="#">
<input type="hidden" id="MyBaseUri" value="http://superuser.com"/>
</form>
Add <base href="http://www.domain.com/"> to your <head> tag. This will make all relative links start from the directory given as href. Then use relative links like support/index.php not beginning with/ (i.e. not /support/index.php)
Note: Make the <base> tag the first tag in your <head> section, as all links after that will be interpreted from that base dir. (e.g. <link href="relative/path"> will already use the base dir if it is defined above.
Advantage: you can move your whole page to a subdirectory like http://www.domain.com/page and only have to change the <base> tag. If you use links like /support/index.php they will always start from the root directory (i.e. http://www.domain.com/)
Dynamic base dir for url rewrites:
<?php
if (preg_match("/https/i",$_SERVER["SERVER_PROTOCOL"]))
$protocol = "https";
else
$protocol = "http";
echo '<base href="'.$protocol.'://'.$_SERVER["HTTP_HOST"].dirname($_SERVER["SCRIPT_NAME"]).'/">';
?>

Categories