I have a WP install in a subdirectory, but in my page templates and php files, I refer to the document root, so it reverts back to the main directory to load image files. Is there a quick way like with an .htaccess file to have the images redirected to the subdirectory first?
MainURL: mysite.com/wordpress1
CPURL: mysite.com/wordpress1
<img src="/images/image1.jpg">
I want the image to load from: "/wordpress1/images/image1.jpg"
I don't want to manually hardcode the subdirectory just in case I don't need to use it in the future. I want it to be universal...
Using .htaccess is not the best way to do this - there are a range of functions that Wordpress defines in order for you to get the correct directories. It's designed for circumstances just as this: where the folder Wordpress ends up in could be very different!
This function will always return the site URL: home_url(), eg: http://example.com/wordpress
So you can reference your images like this:
<img src="<?php echo home_url(); ?>/images/image1.jpg">
There's also a site_url() function, the differences are explained in this question/answer.
Also useful if you're developing a theme, this function will always return the theme directory root: get_template_directory_uri(), eg: http://example.com/wordpress/wp-content/themes/mytheme
There's a full list of functions that return various URLs and server paths for your Wordpress install at this Codex page.
Probably if you're using absolute paths in your templates ther's something wrong in how you write your templates, BTW here is a simple rewrite rule
RedirectMatch 301 ^/wp-content/uploads/(.*)$ /subdir/wp-content/uploads/$1
Replace subdir with your sub directory, this example is made on the uploads subdirectory of wp-content, but you can also apply to other subdirs.
Related
I am attempting to integrate a Wordpress blog page into my (php) website. So far I have been successful in creating an installing it into a subdirectory. I can see that this works.
The issue I am facing is that images are not showing (the alt text is). The issue is that Wordpress uses a incorrect path to the flies:
Wrong Path: https://sitename.com/wordpress/blog/images/brand/logo.png
Correct: https://sitename.com/images/brand/logo.png
It should not have /wordpress/blog/in it.
How do I go about resolving this to point at the correct directory without duplicating the files. Note that in the code the path is is written as: /images/brand/logo.png
If your image folder lays inside theme directory, then you can use
<img src="<?php bloginfo('template_url'); ?>/images/brand/logo.png" />
If your image folder lays outside the theme directory, then you may use
<img src="<?php echo home_url(); ?>/images/brand/logo.png" />
Put this in your htaccess in your root directory.
RewriteEngine On
RewriteRule ^wordpress/blog/images/(.*)$ /images/$1 [L]
That should rewrite those urls.
Depending on your needs, you may also want to consider this article. It doesn't sound like your problem is that you want to change all urls, only the urls to your images.
If you already have path as /images/brand/logo.png , just go 2 levels up like this;
../../images/brand/logo.png
If you are using a wordpress default theme/img upload folder then wordpress build in function get_stylesheet_directory() would work for you. In this case no matter where you shift your project it will always grab this path, this much better then hardcoding path every time.
I am trying to turn my blog URLs into a more SEO friendly format using mod_rewrite. All of my articles are stored in a simple MySQL database. Each blog article url looks like this:
http://www.test.com/blog?id=20&category=online%20php%20tutorials&pagename=how%20to%20customize%20functions
I have managed to to make them look like this using mod_rewrite:
http://www.test.com/blog/online-php-tutorials/how-to-customize-functions/20
Here is my code that I paced in my .htaccess file:
RewriteRule ^blog/([a-z0-9\-]+)/([a-z0-9\-]+)/([a-z0-9\-]+)/?$ /blog?id=$3&category=$1&pagename=$2 [L]
So what happens is this: When I click on the URL http://www.test.com/blog/online-php-tutorials/how-to-customize-functions/20, all of my CSS and images are not loading because it is trying to load them from a directory that does not actually exists. How would I load the files without having to create multiple directories that contain my sites CSS files and images?
Use root identifier / in your path. This will point the DocumentRoot of your server. Let me explain How this works
For an image like this:
<img src='test.jpg' />
Browser/Server will find it as http://www.test.com/blog/online-php-tutorials/how-to-customize-functions/20/test.jpg' but if you use / before the path
<img src='/test.jpg' />
It will look for it in http://www.test.com/test.jpg
Another Technique
Use full path in your files like:
<img src='http://test.com/test.jpg' />
You will want your CSS paths to be based on the root of your domain. If your CSS file is at http://example.com/includes/style.css, you should use /includes/style.css to include that file instead of includes/style.css.
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')
This is hard to explain, so hopefully I'm understood in my question.
(1) I want to create "SEO friendly" links that remove the query string from a web site. There is only one variable, let's call it "page". Here is the following code for my .htaccess file.
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)$ index.php?page=$1
This works in providing the proper redirect. So /applications/ will send to index.php?page=applications.
(2) My index.php will include a view page based on the value of $_GET['page']. Here is some sample code below:
switch ($_REQUEST['page']) {
default:
include ("home.php");
break;
case "apps":
include ("apps.php");
break;
}
There seems to be no problems so far.
(3) Let's make apps.php an exact copy of home.php. home.php loads just fine, but apps.php will not load linked CSS and JScript pages. When apps.php is loaded, it thinks it is in the /apps/ directory. To load the linked pages, I would need to insert a "../" in front of the file name. Then it displays correctly.
So my question is -- How can I properly write the .htaccess file so the home.php and apps.php page can be identical files and produce identical results, instead of the apps.php file being treated as if it were in the /apps/ directory?
First, I should apologize as I don't have a solution which involves making changes in the htaccess. My solutions are of a different nature.
I think the problem can be solved if you have a config variable,preferably in a config file, which will hold the root folder for images, js etc. Most of the time its public_html, the document root, where the url of your website points to. so your config variable could look like:
$base_url = 'http://www.mywebsite.com/';
The config file should be included in index.php unconditionally.
So, when you include any js or images, you do it like this:
<script type="text/javascript" src="<?php echo $base_url;?>js/global.js" />
<img src="<?php echo $base_url;?>images/gradient_green.jpg" />
If you include the config file in index.php, all the files you include based on switch-case conditions, will be able to use the $base_url variable.
Another possible solution is to use the base tag. Look it up here:
http://www.w3schools.com/TAGS/tag_base.asp
I hope this helps.
use absolute urls for js, css and images on your pages (starting with a slash).
/js/main.js instead of js/main.js
You can't do that with .htaccess unless you do an external redirect (by adding the [R] flag to your RewriteRule). But then you expose the query string, which is what you wanted to avoid in the first place.
The reason it can't be done: It is not apps.php which "thinks it is in the /apps/ directory" - it's the browser which "thinks" that. In the page source generated by apps.php, you send relative URLs back to the browser, and now the browser will request these resources relative to the location of the page it asked for. For the browser, the page it got is in /apps/, no matter what rewriting you applied internally on the server side.
So the options you have are:
Do an external redirect with your .htaccess (and defeat your original purpose ;-)
Change the URLs dynamically with PHP while processing apps.php etc, as you said (prefixing ../ to the URLs)
Use absolute URLs, just as #nobody has suggested in his answer.
The last one is the only real option IMHO.
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.