htaccess rewrite to create friendly urls - php

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

Related

url rewrite not working on multiple and dynamic values

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

Sef URL CSS Problems

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.

Apache returns page without style only text and images

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.

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

CSS dissapears when adding trailing slash to URL

The problem is that when I add a trailing slash at the address bar url localhost/register.php/ the CSS dissapears (is not applied anymore). CSS is in a separate file in a separate dir. Here is the structure:
CSS is invoked in header.html with <link rel="stylesheet" href="css/style.css" type="text/css" media="screen" />
header.html is included in index.php with include 'includes/header.html';
Running apache under Windows 7.
When a trailing slash is added, your browser assumes register.php to be another directory, rather than a file. When relative URLs are specified, the external resource will be sought relative to the subdirectory register.php/ (because of the slash).
Example:
Before adding a slash:
css/style.css > http://localhost/css/style.css
After adding a slash:
css/style.css > http://localhost/register.php/css/style.css.
Fixing
To fix this issue, make use of absolute URLs. Either of the following:
<link href="/css/style.css" ... />
<link href="http://localhost/css/style.css" ... />
<base href="/register.php" /> (this tag has to be specified within the <head>

Categories