When I enable the rewrite engine the javascript, css and the images are not included properly, I get error 404 in the console. I'm including the files like that:
<link rel="stylesheet" href="templates/xtc5m/css/mobile.css">
<script src="templates/xtc5m/javascript/jquery.mobile-1.4.2.min.js"></script>
I also tried with including them with a slash in the beginning but then I get error 500
<link rel="stylesheet" href="/templates/xtc5m/css/mobile.css">
<script src="/templates/xtc5m/javascript/jquery.mobile-1.4.2.min.js"></script>
The rewrite rule I'm using is:
RewriteRule (.*)--(.+)\.html$ /product_info.php?products_id=$2 [qsappend,L]
The final url which is wrong looks like that:
http://www.website.de/category/subcategory/name/templates/xtc5m/mobile.css
Use <base> tag on your page header with your root url.
<base href="http://www.example.com/">
Related
Currently, I have this line in my .htaccess file:
ErrorDocument 404 /someProject/404.php
If user enters something like this:
localhost/someProject/gehagae, then it works, it redirects him to 404.php.
But if user enters this: localhost/someProject/index.php/gehagae, it shows only the html of index page without css, images and javascript.
Same thing happens with every page, so how do you redirect it to 404.php?
try to write the full path of your 404 page
for example ErrorDocument 404 http://YOURURL
and make sure in your CSS files you write the absolute path not the relative
for example, if you write it like this
<link rel="stylesheet" type="text/css" href="style.css" />
change it to this one
<link rel="stylesheet" type="text/css" href="/style.css" />``
I try to create Search Engine Friendly URLS like this;:
RewriteRule ^m/([0-9]+)$ my.php?id=$1
However, it gets the page without CSS.
If I write like this:
RewriteRule ^([0-9]+)$ my.php?id=$1
it gets the page with CSS without any problem.
How can I solve it?
When you write CSS references like this:
<link rel="stylesheet" type="text/css" href="style.css">
it's a relative URL. On http://example.com/, that points to http://example.com/style.css, but on http://example.com/my/1 it points to http://example.com/my/style.css, which doesn't exist.
Using an absolute URL fixes this:
<link rel="stylesheet" type="text/css" href="/style.css">
This can be fixed by adding a base attribute to the head tag of each page:
<base href="http://www.example.com/" />
or add / before the css.
<link rel="stylesheet" type="text/css" href="/stylesheet.css">
What was happening was that when you were redirecting the page, it was also looking for the css in my/css instead of css adding the above will fix that.
When i use .htaccess and mod_rewrite to redirect my url with query strings,
for example: http://www.mysite.com/index.php?group=A&id=23
to a Url like this: http://www.mysite.com/index/A/23
Does this change the path on my server, with is used to link to .css and .js files?
So when i have a main.css in the same folder as index.php, do i have to change the link to this file to like:
<link href="../../main.css" rel="stylesheet" type="text/css">
Or can i stay with this:
<link href="main.css" rel="stylesheet" type="text/css">
Yes that changes the path that the browser will send. The browser does not know about your mod_rewrite rules at all.
I'm dealing with some strange behavior that I don't understand.
So if I type:
http://localhost/site/index.php
The http server will return a page with no problems at all, a slash after the index.php will generate a page without any style!
http://localhost/site/index.php/
How can i solve this?
The beneath CSS link isn't working why?
<link rel="stylesheet" type="text/css" href="/css/fonts.css" />
This will work for the slash but if I add something after the slash then I get the all website without styles.
<link rel="stylesheet" type="text/css" href="http://localhost/site/index.php/css/fonts.css" />
EDIT:
All of the requested url are OK after adding the complete path on the href's with this function:
function baseUrl(){
return ("http://".$_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']));
}
Anyway, I've added some rules to the .acccess so now I can't type slash after the index.php but if I type something after the slash everything falls apart :(
Check if your <link /> tags href attributes and <img /> tags src attributes are prepended with /:
Bad:
<link rel="stylesheet" type="text/css" href="css/style.css" />
<img src="img/image.png" />
Good:
<link rel="stylesheet" type="text/css" href="/css/style.css" />
<img src="/img/image.png" />
This way you are certain that browser will compute correct path.
It's nothing wrong with Apache. Your document is using relative URLs.
For example, you specify href="myimage.png" instead of /myimage.png
Changing the base URL breaks these relative URLs.
This is likely a problem with absolute and relative paths in your css <link />. Make sure to use an absolute path.
This is probably related to how you define your references to the CSS, images, etc. If they are defined as relative paths, you are probably in essence looking for them in a wrong directory.
You probably have Apache set to ignore the trailing slash (and just serve index.php rather than look for an index file in index.php directory). The client browser however does not know this and is thinking it is referencing something in a directory called index.php and looking for these files relative to that directory.
To correct this, you should either configure Apache not to ignore the trailing slash (and give a proper 404 error), or implement a rewrite rule to clean up the trailing slash.
The following rule works, but it changes the URL in the address bar, which is not intended.
RewriteRule ^network/(.*)$ http://www.example.com/network.php?networkUrl=$1 [L]
The following rule redirects, the URL stays the same, but all the images, includes in the network.php file become referenced incorrectly...
RewriteRule ^network/(.*)$ network.php?networkUrl=$1 [L]
Is there a way to make this work?
This is because your browser interprets paths as relative.
To solve this reference your images and CSS with absolute paths, i.e. <img href="image.jpg" /> becomes <img href="/image.jpg" />
Same applies for css so
<link href="stylesheets/foo.css" media="print" rel="stylesheet" type="text/css"/>
becomes
<link href="/stylesheets/foo.css" media="print" rel="stylesheet" type="text/css"/>
In this way all resources links works as expected when referenced from any depth as /foo/bar/baz/script.php and so on.
Setting a base href HTML tag in your page could help too:
<base href="http://www.domain.com/" />
then all your relative images, stylesheets or javascript files will be relative to this base href.
http://www.w3.org/wiki/HTML/Elements/base