I have this in my .htaccess :
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule . /index.php
and this is how i work with the parameters :
<?php
$request = $_SERVER['REQUEST_URI'];
$request = substr($request,1);
$params = explode('/', $request);
$safe_pages = array('page1','page2','page3');
if(in_array($params[0],$safe_pages))
{
include($params[0].'.php');
}else
{
include('404.php');
}
?>
Let's say my site is : www.site.com
www.site.com/page -> works perfectly
www.site.com/page/parameter -> doesn't load any css or images because it looks for them in root/page instead of root/
I tried to use absolute links for my css and images but it still didn't work, so i guess the problem is in the .htaccess or i'm missing something.
The problem lies not within htaccess rewrite rules, but in your html - yuor links to static content (like css, images or scripts) should start with slash to be ralative to the domain. So instead of
<link href="css/style.css" />
<script src="js/script.js"><script>
Use
<link href="/css/style.css" />
<script src="/js/script.js"><script>
Other way is to use base tag but it can break named anchors on your page
Use
<base href="http://absolute_path_here/">
in your HTML before the closing </head>.
Related
I have a website too extensive editing the URLs of styles css and files js, images would cost me too much time.
I'm working at localhost
localhost/project/index.php
The friendly url I want to have:
localhost/project/index/
From this url
localhost/project/online.php
to this
localhost/project/online/video/hd/free/
I have observed this website http://crazycafe.net/demos/seo/ and its source code, the styles css is maintained this way: <link href="css/bootstrap.min.css" rel="stylesheet" type="text/css">
without adding an absolute URL to styles css and their files and images.
How do i create a friendly url this way?
If the user modifies the url in this way:
http://crazycafe.net/demos/seo
Redirect to this way:
http://crazycafe.net/demos/seo/
On the other hand like conserving the styles css the files js and images, without having to modify to an absolute route.
My file directory is:
assets/css/style.css
assets/js/app.js
assets/fonts/icons/image.png
assets/fonts/ttf/roboto.ttf
assets/img/system/image.png
assets/img/logo.png
Note:
I found this question in SO, of how conserve CSS styles.
But I do not understand the use of friendly URL - .httaccess
You need to switch on mod_rewrite and use it. Example:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^folder1.*$ http://example.com/ [R=301,L]
Folder-based example:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^folder1/(.*)$ http://gs.mt-example.com/folder2/$1 [R=301,L]
Your case:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^online.php$ project/online/video/hd/free/$1 [R=301,L]
See more here.
if request to my site goes like:
my1stdomain.com/request-1/
my1stdomain.com/any-another-request/
it should open web-pages:
my2nddomain.com/folder/request-1.php
my2nddomain.com/folder/any-another-request.php
I put php file in the web-root of my1stdomain.com with name referer.php
and it looks like:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} /$ [OR]
RewriteCond %{REQUEST_FILENAME} (html|php)$
RewriteCond /home3/juraj1/public_html/referer.php -f
RewriteRule ^.*$ /referer.php [L]
</IfModule>
and referer.php looks like:
<?
$login=preg_replace('/\//i','',$_SERVER['REQUEST_URI']);
$html=file_get_contents('http://my2nddomain.com/folder/'.$login.'.html');
echo $html;
?>
but it returns only html content without css styles and images
Can you please help me with it?
I assume the HTML files on my2nddomain.com link to the css files/images in a relative way, like <img src="images/picture.jpg" />. But since this image does not exist on my1stdomain.com, the browser doesn't find it.
Solution 1:
Link to all images/css files with an absolute path, like <img src="http://my2nddomain.com/images/picture.jpg" />
Solution 2
Add the <base> tag to the HTML head of all the files on my2nddomain.com like this: <base href="http://my2nddomain.com" />. This can be achieved by changing the HTML on the fly like $html = str_replace('</head>','<base href="http://my2nddomain.com" /></head>',$html);
There are also other solution with rewriting every request to a css file/image on your 1st server to the corresponding file of the 2nd server, but this will be more complicated.
I have the this project:
In the index I check the url with this code:
if( isset( $_GET['url'] ) ) {
if( file_exists( 'classes/layout/'.$_GET['url'].'.php' ) ) {
require_once 'classes/layout/'.$_GET['url'].'.php';
}
}
And in my .htacces this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^(.*?)$ index.php?url=$1&%{QUERY_STRING} [L]
This works great. When is go to example: 127.0.0.1/test/pages/edit the index includes edit.php but in the index i use a css file named: test.css wich make the whole html background red.
If i go to 127.0.0.1/test/ i see the whole background red. But when i go to 127.0.0.1/test/pages/edit it is white. I checked the urls of the css file and i get this:
127.0.0.1/test/ = 127.0.0.1/test/cache/css/test.css
127.0.0.1/test/pages/edit = 127.0.0.1/test/pages/cache/css/test.css
Does someone know how to fix this?
You need to either make the css links absolute (starts with a /test/) or add a base for all your relative links by adding this to your page header:
<base href="/test/">
(the base URI may need to be adjusted, but it looks like you want it to be /test/)
The reason this is happening is because the browser will guess what the URI-base is depending on the URL that's loaded. When you put http://127.0.0.1/test/ in the browser, the browser assumes the URI base is /test/ and all relative links will have that appended to the front. But when you put http://127.0.0.1/test/pages/edit in your browser, it assumes the base is /test/pages/ and thus your relative links get the wrong base appended to the front.
The problem is that u did not give the proper path to the css. Always try to call style/script from base url as below:
Hope this helps.
<?php
if($_SERVER['REMOTE_ADDR'] == '127.0.0.1')
define("SITEROOT","http://localhost/test/"); // base to your web directory ie,www or htdocs
?>
//call style in this manner
<link href="<?php echo SITEROOT; ?>Sourcefiles/cache/css/test.css" rel="stylesheet" type="text/css" />
In my .htaccess file i have the following re-write rule
RewriteRule ^([a-z]*)$ work.php?album=$1 [L]
I am reading all directories in root directoy and creating links like
code in PHP
$dir=glob('images/works/*');
$dir_listing=array();
foreach($dir as $list)
{
if(is_dir($list))
$dir_listing[]= (basename($list));
}
foreach($dir_listing as $folders )
echo"<a href='$folders' ><img src='Default-thumbnail-folder.jpg' /> </a>";
the problem is i have three folders under works directory {alb,ban,bas}
the first and last {alb and bas} work properly for my redirect rule URL{siteName/works/bas}
but the second {ban} creates a URL like {siteName/works/ban/} Notice the extra "/" is creating the problem, it seems for some folder names it is creating the problem, so please help me in creating a proper redirect rule
i want a link like
<a href="alb" >About</a> // to redirect to work.php?content=about
I also notice that if URL in browser is like work/alb/ CSS is not applied
i tried
<link href="sample.css" rel="stylesheet" type="text/css" />
and
<link href="css/sample.css" rel="stylesheet" type="text/css" />
it does not work for URL like {works/about/} it works for {works/about}
Easy!
Try to change your rewriterule to:
RewriteRule ^([a-z]*)/?$ work.php?album=$1 [L]
So that the "/" at the end may or may not be present, in both cases it's ok.
to avoid rewriting css URI's use these rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
as for your other problem you can use a trim on the result url to make sure you never have a "/" at the end
trim($url,'/');
I am trying to apply css in the Zend Framework. My css file is in public/css/layout.css. First of all I added the following code lines in bootstrap.php:
protected function _initPlaceholders()
{
$this->bootstrap('View');
$view = $this->getResource('View');
$view->doctype('XHTML1_STRICT');
// Set the initial title and separator:
$view->headTitle('My Site')
->setSeparator(' :: ');
// Set the initial stylesheet:
$view->headLink()->prependStylesheet('../../css/layout.css');
}
Which simply specifies the title and stylesheet to be used. Then in layout.phtml I add the following code:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<?php
echo $this->headTitle();
echo $this->headScript();
// add a link to the site style sheet
echo '';
echo $this->headLink();
?>
</head>
Which simply adds link of the stylesheet specified in bootstrap.php.
But when I run the project then I am getting the form without css. Why is this? When I check the css with Firebug in a Mozilla browser it says:
<h3>Exception information:</h3>
<p>
<b>Message:</b> Invalid controller specified (css) </p>
<h3>Stack trace:</h3>
<h3>Request Parameters:</h3>
<pre>array (
'controller' => 'css',
'action' => 'layout.css',
'module' => 'default',
) </pre>
Please help me out to resolve this issue.
Try
$view->headLink()->prependStylesheet('/css/layout.css');
this should help.
Do you have this code on your /public/.htaccess file?
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
If you don't have the first 4 lines, this file will redirect your request of "css/layout.css" to index.php and then, Zend will interpret it as a link for a controller -> action.
If you have this file, make sure that mod_rewrite is enabled on your server.
And you sould put your link as "akond" said.
Good luck =)
This helped me:
Zend_Controller tries to execute my stylesheets as controller
[update]
Ok, just added the following line to my .htaccess file and all works as expected now...
RewriteRule !.(js|ico|txt|gif|jpg|png|css|htc|swf|htm)$ index.php
but then I found this one:
http://devzone.zend.com/1651/managing-css-and-javascript-files-within-a-zend-framework-app/