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.
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 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.
I have this htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ dl.php?id=/$1 [QSA,L]
</IfModule>
it works perfect. I just have one problem, I insert my site urls and images like these
<img src="img/image.gif">
<a href="do.php">
its short url not full like
<a href="mysite.com/img/image.gif">
so when now I open 127.0.0.1/dl.php/1/ I can get
$_GET['id']
But my links and images want to open from
http://127.0.0.1/dl.php/img/h.png
but it must be
http://127.0.0.1/img/h.png
can anyone help me with this? I can't change all urls in my site and make them full url. I have like 50 page and I want this htaccess just for dl.php file
It wants to do this because you're using relative paths in your src.
Simply prepend a / to your paths, and it will go to the root of your site (ie. relative to the domain), rather than relative from the current folder (of course, not a real folder, but according to the URL your browser doesn't know any better).
<img src="/img/image.gif">
<a href="/do.php">
Sorry to say but you will need to change each path. There is one alternative, but you'd still have to change every path unless dl.php contains like a header/footer. In this case, you can append a <base href=".." /> tag to the header, which will force relative paths to be resolved relative to the path you give it.
<head>
...
<base href="http://www.mysite.com/" />
</head>
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.