url rewrite not working on multiple and dynamic values - php

This is the .htaccess:
RewriteEngine on
RewriteRule ^contactus\/?/?$ index.php?goto=contactus
RewriteRule ^home\/?/?$ index.php?goto=home
RewriteRule ^event\/?/?$ index.php?goto=event
RewriteRule ^album\/?/?([a-z0-9-]+)\/?/?$ index.php?goto=home&albumid=$1
The problem is on last RewriteRule, first three are working as they are supposed to. Problem is in last one, when i open URL like http://localhost/album/56c9eb6b1fe75,it doesn't work properly. But when i try this: http://localhost/index.php?goto=home&albumid=123then this works good
PS:
By doesn't work properly I mean: , and.
Check the address bars in both images to understand the problem. In first one i think the bootstrap and other css files are not loaded.

With your update I believe the issue is that you are using relative paths on your page. This makes your CSS, JS, and all other resources (images) load from the current path /album/. You should add a leading / to your paths so they load from the root of your server.
A call to the stylesheet
<link rel="stylesheet" type="text/css" href="style.css">
will load as
example.com/admin/style.css
if you make it
<link rel="stylesheet" type="text/css" href="/style.css">
it knows to go to the root of your domain.
example.com/style.css

I'd say this is what you need:
RewriteRule ^album/([a-z0-9-]+)/?$ index.php?goto=home&albumid=$1

Related

Subfolder don't load the style.css

I have a .htaccess file that redirects all the URL request to my index.php
AcceptPathInfo On
RewriteEngine on
RewriteBase /Projects/tester/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?request=$1 [L,QSA]
Inside my index.php I have a code that include the URL file:
<?php
if(isset($_GET['request'])){
include('pages/'.$_GET['request'].'.php');
}else{
echo 'index';
}
?>
If my URL is localhost/Projects/tester/green my index will include a file called green.php that exists inside the folder pages.
inside pages/green.php I have this code:
<link rel="stylesheet" type="text/css" href="style.css">
<div id="greenBox">
This box is green!
</div>
When I run that URL I can see a large green div with an text inside.
Great, now lets create a folder called subs inside the folder pages and create a file called yellow.php
<link rel="stylesheet" type="text/css" href="style.css">
<div id="yellowBox">
This box is yellow!
</div>
When I run this URL localhost/Projects/tester/sub/yellow I can see the text but the style.css don't load (I can't see the yellow div).
If all the content is being included in the index.php and style.css file is in the same folder as the index.php, why green.php can load the style.css but the yellow.php not?
I can see the yellow div only if I add ../style.css inside the yellow.php, but if it is to work on this way, I believe we should use ../style.css on green.php while in yellow.php use ../../style.css, no?
Is there a way to solve this? is problem in .htaccess?
Of course the external browser doesn't understand where your files are located on the server, so if you are in the following situation (from the browser's perspective):
http://localhost/Projects/tester/green
-> hey browser, load "style.css"
-> browser loads http://localhost/Projects/tester/style.css
But if you change the "virtual" path:
http://localhost/Projects/tester/sub/yellow
-> hey browser, load "style.css"
-> browser loads http://localhost/Projects/tester/sub/style.css
So you have three solutions here, in order of my personal preference:
1) Use absolute URLs for loading css data, optionally using a PHP var:
<link rel="stylesheet" type="text/css" href="/Projects/tester/style.css">
<link rel="stylesheet" type="text/css" href="<?php echo $base_path; ?>/style.css">
2) Use the <base> html element in head: (warning: it applies to ALL relative urls in the page: links, images, etc.)
<head>
<base href="http://localhost/Projects/tester/" />
</head>
3) If using absolute URLs is not an option, you can calculate how many "virtual subdirectories" you are down and generate a prefix like this:
<?php
// first calculate $virtal_subdirs_number
$relative_urls = str_repeat("../", $virtual_subdirs_number);
?>
<link rel="stylesheet" type="text/css" href="<?php echo $relative_urls; ?>/style.css">
Its because the style.css is in parent directory of yellow.php, So for accessing a file in parent directory you must have to give path like ../style.css which mean look for it in one directory back and /style.css mean in the current directory which contain yellow.php

Path to assets folder on CodeIgniter

I'm coding something into CodeIgniter and I want to apply a css template. I found one I like and download it.
After merging the css code with the code I have before the merging, I found that the images used by the css template doesn't load. Originally they should stay on root of html folder (or www, or public_html, you know what I mean...), but I put them under an assets folder which is on same level as system folder.
Something like this...
Website Folder
|
|---application\
|-----(CodeIgniter application folders...)
|---assets\
|-----style.css
|-----css\
|-------mini.css
|-----images\
|-----js\
|-----robots.txt
|---system
|-----(CodeIgniter system folder...)
|index.php
I googled for a couple of hours and I found this post (post #5). I try what the OP says but it doesn't work.
I can autoload the url_helper adding
$autoload['helper'] = array('url');
to the autoload.php file. But when I add
<base href="<?php echo base_url() ?>"/>
the images are still absent.
The original html file has this line
<link href="style.css" type="text/css" rel="stylesheet" />
so, I'm guessing that I should add assets/ so it looks
<link href="assets/style.css" type="text/css" rel="stylesheet" />
but still the images are missing.
If I move the contents of assets folder to root, everything is fine, but of course, that's not a good practice AFAIK...
My base_url is
http://localhost/prog/nonsense/mvc/
Before you ask, yes, I did read the .htacces solutions, but I really don't want to mess with .htaccess editing for now.
A little help here could be appreciated.
have you check .htaccess ?
It must have something like:
RewriteCond $1 !^(index\.php|assets|upload|robots\.txt|.*\.css)
I have the same thing as you, and I call them like this:
<link href="<?=base_url('assets/style.css');?>" rel="stylesheet">
This works for me if the base_url() is set and the url helper is called.
I call the base_url() and then the assets folder then style.css. There's no need for a / after base_url();?> because there's one in the base_url() anyway.
Another option using codeigniter's built in HTML function:
Also, if you look at this link, you'll see you can use the HTML helper and call it via codeigniter's built in functions.
This time, you shouldn't need base_url();
do:
$autoload['helper'] = array('url', 'html');
then in the view for header add this:
<?=link_tag('assets/style.css');?>
It will output to this:
<link href="http://localhost/prog/nonsense/mvc/assets/style.css" rel="stylesheet" type="text/css" />
base_url could also be used as link_tag:
<link href="<?php echo base_url('assets/style.css'); ?>" rel="stylesheet" type="text/css" />
would work too and keep your code cleaner.
yes! I just added the following line in .htacces file it working fine
RewriteCond $1 !^(index\.php|images|robots\.txt)
I just add one line in .htacces side of application folder
RewriteCond $1 !^(index\.php|images|robots\.txt)

Why is my php file unable to locate external files (image,css...) when using RewriteRule?

I am using following RewriteRules :
RewriteRule ^([a-zA-Z0-9]+)$ page.php?p=$1
RewriteRule ^e/([a-zA-Z0-9]+)$ edit.php?p=$1
The first one works fine : when typing mysite.com/id it loads mysite.com/page.php?p=id on server side.
The second one is working as well : when I type mysite.com/e/id it loads mysite.com/edit.php?p=id (as expected). But in that case edit.php can't loacte any external files like my css file.
<link rel="stylesheet" type="text/css" href="style.css" />
When doing either :
<link rel="stylesheet" type="text/css" href="../style.css" />
Or simply removing the direcory in the RewriteRule like :
RewriteRule ^e_([a-zA-Z0-9]+)$ edit.php?p=$1
it fixes that problem.
Now I don't understand why my edit.php is unable to locate external files even thought it loads on the correct path on server-side (mysite.com/) and not like the url displays in an extra directory (in this case mysite.com/e/ ).
You can use a base url for all the site's paths, like this:
<base href="http://yoursite.com/blog/" />
or
<base href="/blog/" />
And then writing relative paths from the base's:
...
Plus, you have to be extra careful with those "accept all" patterns, as they may conflict with the stylesheets, images and the rest of media if not used properly. Either appending an extension to the patterns or excluding media could be possible fixes.
you have to write full path not the relative path when using mod_rewite
References
apache mod rewrite
one more
beginners guide

REQUEST_URI of a php document is "favicon.ico" or a stylesheet's filename

I log traffic coming in to my site. I get a user's landing page by looking at the REQUEST_URI of the first page they come to. The site is all php so every page includes a file that has, among some other things:
$_SESSION['landing']=$_SERVER['REQUEST_URI'];
I also log traffic going out. I've noticed two kinds of entries for $_SESSION['landing'] though that don't quite make sense. I'll see either: "favicon.ico" or "ie8.css"
Favicon is just a favicon:
<link rel="shortcut icon" href="http://www.SITE.com/images/favicon.ico" />
ie8.css is a stylesheet that fixes a couple of things so they'll look right in IE8. I include it like this:
<!--[if lt IE 9]>
<link rel="stylesheet" type="text/css" href="/ie8.css" />
<![endif]-->
Why would these show up as the $_SERVER['REQUEST_URI'] for a php document?
The are 3 reason i know that can cause such error ...
Error URL redirection .... when trying to implement clean URL ....
`AddType x-httpd-php .css .ico .xyz
Someone is messing with your server ....
I hope this helps
This for steering me in the right direction. I was using a rewrite rule to simulate a "county" directory e.g. :
/county/Sonoma?1, /county/Sonoma?2, etc.
to distinguish counties from cities which look like /Santa Rosa. This is the rule:
RewriteCond %{REQUEST_URI} !/\.php/ [NC]
RewriteRule ^county/(.+)$ /county.php?county=$1 [L,QSA]
but my link to favicon.ico was relative, not absolute, i.e. favicon.icon instead of /favicon.ico. So apache was looking for /county/favicon.ico instead of /favicon.ico. And the RewriteRule was sending it to county.php.
The upshot was that $_SERVER['REQUEST_URI'] was /favicon.ico.
I prepended the slash and it worked. And for good measure I added another rule:
RewriteRule ^county/(.+/.+)$ /$1 [L]
just in case.

URL rewrite small issue

I try to do the following:
http://abc.com/site/15/mike to http://abc.com/site/profile.php?id=15
In the script folder I've placed the following htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([0-9]+)/(.*)*$ ./profile.php?id=$1
The problem is that the file is found, but into it, the css and other files can't be found.
It considers paths like: http://abc.com/site/15/style.css
What could I do? I've tried so many rewritte rules that I don't know what could be there.
the ideea is that all the link from tht bage are relative t that path. all will become www.abc.com/site/15/onepage.php. So what can I do about that?
Why don't you write your link beginning by a '/' so that the browser knows the right path ?
<link href="/css/myCssFile.css" rel="stylesheet" type="text/css" />
This should exclude urls with a . inside, such as styles.css, it that helps.
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([0-9]+)/([^\.]+)*$ ./profile.php?id=$1
You can just create a new folder called css to be like this
http://example.com/site/15/css/myCssFile.css
or use full path like this
<link href="usr/www/site/15/css/myCssFile.css" rel="stylesheet" type="text/css"/>
Tell me if there's a problem

Categories