RewriteRule causes a malfunction of the other path (subdir), css/imgs/js - .htaccess

Like title, that rule:
RewriteRule pagtwo/pic/(.*) index.php?pic=$1 [PT,QSA]
Rule working, but not the file included by subdirectory for example
<link type="text/css" rel="stylesheet" href="css/base.css" media="all">
<img src="imgs/image.jpg">
and goes on
Directories are css, imgs and js, why they do not included correctly? Must I insert the absolute path for that directory?

Try adding this to you page header:
<base href="/">
Because you're using relative paths, what would normally resolve relatively from / won't woek when resolving relative from /pagtwo/pic/.

Related

why slim requires full directory path for loading external assets(js/css) file

<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script type="text/javascript" src="/slim/app/views/animations/jquery.min.js"></script>
<script type="text/javascript" src="/slim/app/views/animations/bootstrap.min.js"></script>
<link rel="stylesheet" href="/slim/app/views/animations/bootstrap.min.css">
</head>
<body>
<div class="container">
hello
</div>
</body>
</html>
I have to specify full directory path for loading the js and css files
(jquery.min.js and bootstrp.min.js and bootstrap.min.css).Here i have
(/slim/app/views/animations/) as the directory which contain all the
files. if currently i am in the views directory i should use the
(animations/whatever the file name) but it is not working in these
way. but if i use the full directory path it all works fine. why these
is happening can anyone explain.
(slim) is my root directory where i installed slim
Using Relative paths is not how you typically work with Slim. Your assets (JavaScript/CSS/images, etc) should be referenced absolute from your views:
src="/assets/js/myapp.js"
Routed URLs do not map directly to file system based resources because templates should not be accessed by URL publically - they are only served by the controllers.
A common slim file structure looks like this:
app/
public/index.php
templates/
view.html
assets/
images/
js/
css/
Some add the assets subfolders directly into the public folder.
Others have those folders on the root level.
However, in general, public assets (e.g. CSS, JS, images) should be beneath the public document root (accessible to the public).
One thing that could be helpful if you do not like this behavior is to use Slim's basePath variable.
You should be able to set the base URL as a View variable in a slim.before callback in your index.php so that it is available to all routes like this:
$app->hook('slim.before', function () use ($app) {
$app->view()->appendData(array('baseUrl' => '/base/url/here'));
});
or
$app->hook('slim.before', function () use ($app) {
$posIndex = strpos( $_SERVER['PHP_SELF'], '/index.php');
$baseUrl = substr( $_SERVER['PHP_SELF'], 0, $posIndex);
$app->view()->appendData(array('baseUrl' => $baseUrl ));
});
And then apply it to the references in your HTML tag of the base template file and that's it.
<link rel="stylesheet" type="text/css" href="{{ base_url() }}/css/style.css" />
Or use Uri::getBaseUrl(), e.g.
$basePath = $request->getUri()->getBasePath();

htaccess make alias and rewrite rule

I have html in /html/test-html/index.html here. There are also css and images like this one: /html/test-html/css/style.css
When I open index.html in browser styles and images are loaded correctly.
When I'm trying to render it's contents by php echo function I get problems with 404 error, because paths in index.html and style.css are relative, like this one:
<link src="css/style.css" rel="stylesheet" type="text/css" />
So when I echo index.html contents in my script (i.e. by url like http://localhost/product1) browser tries to find style.css by this url:
http://localhost/css/style.css
But actually file is located in
/html/test-html/css/style.css
I think that good idea is to simulate URL like browser in http://localhost/html/test-html/, but actually we are in http://localhost/product1 (with RewriteRule), but without a redirect.
In other words, I want browser to think that http://localhost/product1 is http://localhost/test-html and server to think that http://localhost/product1 is http://localhost/index.php?r=product/view&id=1
Is it possible?

Solving shortening url with htaccess

I have rewritten my url using htaccess .After navigating to the page, the layout distorts and no value fetched from the database is printed out.This is the main URL
http://www.deffsale.com/personalBusiness/PersonalBusiness.php?item=ladys%20Fashion&page=1
and I'm navigating to this page using this one
http://www.deffsale.com/personalBusiness/all/ladys-Fashion/1
This is my ht-access
RewriteEngine On
RewriteRule ^About-us AboutUs.php [NC,L]
RewriteRule ^personal-business personalBusiness [NC,L]
RewriteRule ^personalBusiness/all/([0-9a-zA-Z_-]+)/([0-9]+) personalBusiness/PersonalBusiness.php?item=$1&page=$2 [NC,L]
please help me solve this because it has been a disturbance past three months
You need to include this line just below <head> section of your HTML:
<base href="/personalBusiness/" />
so that every relative URL is resolved from that base URL /personalBusiness/ and not from the current page's URL.
It is a path issue with your CSS and images related to the rewrite rules. Add a rule like this before the other rules:
RewriteRule \.(js|css|jpe?g|png|gif)$ - [L]
which will allow those requests to be passed unmodified.
In addition, make sure the URLs for JavaScript, CSS and image files are coded relative to DocumentRoot. For example if your files are organized like this:
/ - index.php
+ css
+ base.css
+ color.css
+ images
+ logo.png
The style tags would be:
<link rel="stylesheet" type="text/css" href="/css/base.css">
<link rel="stylesheet" type="text/css" href="/css/color.css">
And images would be:
<img src="/images/logo.png" alt="logo">
As the other respondent suggested, you may use a base tag with relative URLs instead. In that case, you would code the tags like:
<base href="/">
<link rel="stylesheet" type="text/css" href="css/base.css">
<link rel="stylesheet" type="text/css" href="css/color.css">
And images would be:
<img src="images/logo.png" alt="logo">
The difference is that the base tag can be used to set the reference point for the relative URLs. Bear in mind the base tag will affect all relative URLs on your page.

rewrite friendly url with .htaccess issue

I want to make Friendly URLs, but when I try my PHP code works correctly but my CSS and JS files are not loaded correctly. It tries to get these files from different path.
I'm using WAMP, and I've created bsp directory for my project.
There are two folders for js and css files and I've used the css and js files in my php pages like
< script type="text/javascript" src="js/menu_config.js">< / script >
< link rel="stylesheet" type="text/css" href="css/menu.css" / >
and the .htaccess file code is here:
RewriteEngine On
RewriteRule ^products/([0-9]+)_([0-9]+).html$ products.php?maid=$1-$2 [L]
But when I browse through url http://localhost/bsp/products/2_7.html,
It does not load the js and css files.. at that time the path of js and css files becomes
http://localhost/bsp/products/css/menu.css etc
the css and js path should be http://localhost/bsp/css/menu.css.
I hope anybody will understand this problem as soon as possible.
This is just to do with your paths when including the files. You're currently using relative links, which means that "js/menu_config.js" will be appended onto the current URL.
Instead, you need an absolute path. By prepending your URLs with a /, you denote that you want to specify the path relative to the document root (rather than the current subdirectory). I'll leave out what the paths should be for now, filling them in is an exercise for you.
This should work
< script type="text/javascript" src="/js/menu_config.js">< / script >
< link rel="stylesheet" type="text/css" href="/css/menu.css" / >

mod_rewrite .htaccess - Stylesheets mess up

I'm trying to rewrite some URL's on my website, and it works fine, I just have some issues with the stylesheets. All stylesheets works at index.php but not when url's like: localhost:8888/folder/path/(1,2,3,4)
I have my stylesheets in a file where they're listed like this:
<!-- CSS -->
<link rel="stylesheet" type="text/css" href="includes/css/site.css"/>
<link rel="stylesheet" href="includes/css/viewProfile.css" type="text/css" media="screen" title="no title" charset="utf-8">
<link rel="stylesheet" href="includes/css/viewProvider.css" type="text/css" media="screen" title="no title" charset="utf-8">
Is there a way to fix this with mod_rewrite or how would I fix this?
This is because you use a relative path to the style sheets. Note that the browser sends requests to
localhost:8888/folder/path/includes/css/...
in that case. If the styles always reside in that configuration at the server root, you could simply make the relative URI an absolute one. Or you remove the path portion for CSS files on the server side.

Resources