Sef URL CSS Problems - php

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.

Related

Dynamic link in css stylesheet with PHP variable

I'm building a dynamic page template for wordpress (which means it has to work at least 4 different domain without changing anything like links etc.) and I added a custom css link like this:
<link type="text/css" rel="stylesheet" href="https://mywebsite.com/wp-content/themes/mytheme/css/page-food.css">
I can reach the server home in a variable like $home_path. Or the theme main folder like $theme_path. So is there any possible way to add the href a PHP variable?
I mean
<link type="text/css" rel="stylesheet" href="$theme_path/css/page-food.css">
just add php start and end tags there
<link type="text/css" rel="stylesheet" href="<?=$theme_path?>/css/page-food.css">
wordpress css call in daynamic path
<link href="<?php echo get_template_directory_uri(); ?>/css/fonts.css" type="text/css" rel="stylesheet">

PHP - Including HTML from different folder

I am currently working on a admin backend and now want to move some pages into different folders (so I do not have all scripts in one folder).
Structure looks like this:
/
/theme/assets/...
/templates/head.php
/templates/foot.php
/top/index.php
/index.php
In head.php there are some stylesheets added like this
<link href="theme/assets/global/plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
This works fine for files in the root folder (e.g. index.php), but when I use the same head.php in /top/index.php it does not work because the path is wrong.
Is it somehow possible to properly include the stylesheets etc. while having the scripts in different folders?
Greetz,
Just specify the root identifier (/) at the beginning of the href attribute:
<link href="/theme/assets/global/plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css" />
Try this,
<link href="../theme/assets/global/plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
In /top/index.php
OR
<link href="<?php echo $_SERVER['DOCUMENT_ROOT'] ?>/theme/assets/global/plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css" />
This code can be used in any page. The $_SERVER['DOCUMENT_ROOT'] points to the root.
Use '../' to reference the folder above and work your way back.
e.g
<link href="../theme/assets/global/plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">

Rewrite engine messes up javascript and css

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/">

does Using clean URL's with query strings change the path/directory

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.

htaccess rewrite to create friendly urls

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

Categories