.htacces Appears only html with no CSS or PHP - php

I've been wondering on the internet on how to work with htacces(really hard to learn it). And when I was lurking in the internet, I found this: http://www.generateit.net/mod-rewrite/
Well, I inserted my url(working on localhost):empresa.com/index.php?p=sub_artigo&id=1&cat=Mercearia
and it gave me this(with all options by default):http://empresa.com/sub_artigo/5/Mercearia.html
And the .htacces code was this:RewriteRule ^([^/]*)/([^/]*)/([^/]*)\.html$ /index.php?p=$1&id=$2&cat=$3 [L]
And when I generate the url in php I do
$response2[nome_sub_artigo]
And then, when I click the button, it appears like, only html.
example: http://s14.postimg.org/wr137fx4x/htacces_error.jpg
Any idea what is happening ?

It looks like you are using relative links for your assets (images, javascript, css).
That means that when you look for css/my_stylesheet.css, from the new url, the browser will request a url like http://empresa.com/sub_artigo/5/css/my_stylesheet.css.
The easiest solution is to always use absolute urls for your assets, like /css/my_stylesheet.css, etc.

Your PHP code is outputting relative paths to the style sheets.
Example:
<link rel="stylesheet" type="text/css" href="styles.css">
Given your example input URL, this causes the browser to look for a stylesheet at the following URL:
http://empresa.com/sub_artigo/5/styles.css
This is because the browser doesn't know that the URL has been rewritten - it believes it is viewing a file in a subdirectory that doesn't really exist.
Instead you should use an absolute path, such as:
<link rel="stylesheet" type="text/css" href="/styles.css">
Notice the leading / on the path? This will tell the browser to look from the root of the domain:
http://empresa.com/styles.css
In this way you can still decouple your HTML from the protocol and domain/port (so you aren't tied to http://empresa.com) but the path will always be the same regardless of the URL that was used to reach the referencing page.

Try this code :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\.html$ /empresa.com/index.php?p=$1&id=$2&cat=$3 [L]

Related

Clean URL's using htaccess breaks my css

I'm trying to clean my URL path's so that I don't have any GET parameters and PHP extensions in all of my links. As an example for what I'm trying to achieve:
http://localhost/projectname/?page=dashboard
needs to be:
http://localhost/projectname/dashboard/
And it actually works, I used the following code in my .htaccess:
RewriteEngine on
RewriteCond %{REQUEST_URI} dashboard/
RewriteRule dashboard/ http://localhost/projectname/?page=dashboard
However, the page displays itself without any CSS or Javascript. I tried navigating to my Style.css only to find out that it looks exactly like the webpage itself, instead of showing me my CSS rules.
So what am I doing wrong? Please don't mark my question as a duplicate, I've been looking into similar questions but couldn't solve the problem.
You're changing the relative URI when your URL goes from /projectname/ to /projectname/dashboard/.
When that happens, every relative link on the page will have the wrong base added to it. The browser has no idea that the base is actually /projectname/ when all it sees is the location being at /projectname/dashboard/.
Right now, when your css is linked like:
<link rel="stylesheet" href="Content/style.css">
The browser attempts to resolve it by adding the base from the location, and it'll load:
https://localhost/projectname/dashboard/Content/style.css
which doesn't exist because the "dashboard" isn't actually a folder.
You can either add a base to the actual page by including this in the page header:
<base href="/projectname/" />
Or you can try to un-rewrite the dashboard out of the request (a bit trickier and more error prone) by adding this rule
RewriteCond %{REQUEST_URI} ^/projectname/dashboard/(.*\.css)$
RewriteCond %{DOCUMENT_ROOT}/projectname/%1 -f
RewriteCond ^projectname/dashboard/(.*\.css)$ /projectname/$1 [L]
Note that does this may have an unintended impact on your browser's cache.
Use the absolute path to the CSS.
<link rel="stylesheet" href="http://localhost/projectname/assets/style.css">

After using .htaccess RewriteRule page not opening properly

I am trying to convert my dynamic PHP link to a clear link; for that I used RewriteRule. But the work seems to be not done properly
I am trying to convert
dappersole.in/product.php?product_id=45;
To
dappersole.in/product/46;
Using
RewriteRule ^product/([0-9]+)$ product.php?prodct_id=$1 [NC,L]
After this the page does not load properly. But if I remove product/ from RewriteRule, the page loads perfectly
RewriteRule ^([0-9]+)$ product.php?prodct_id=$1 [NC,L]
dappersole.in/45;
Your page won't load fully because you are referring to your CSS and JavaScript resources relative to the current location. You need to change this to refer to the resources relative to the root of the site. For example, change this:
<link rel="stylesheet" type="text/css" href="css/slider-pro.min.css" media="screen"/>
to this:
<link rel="stylesheet" type="text/css" href="/css/slider-pro.min.css" media="screen"/>
Note the leading slash before css/slider.... You need to do this for all your CSS, jQuery and image resources. Alternatively, you can save time by adding the following to your <head> node:
<base href="/">
Or, even better:
<base href="http://dappersole.in/">
Additionally, I recommend you do the same with your mod_rewrite rule:
RewriteRule ^product/([0-9]+)$ /product.php?product_id=$1 [NC,L]
Lastly, I recommend you ensure Multiviews is not enabled, by adding this line to the top of the file:
Options -Multiviews
I too have had similar issues to this with Apache 2.2 - if the name and the rewritten name are very similar it fails. You could cheat slightly and use a different url such as http://www.example.com/item/46
RewriteRule ^item/([0-9]+)$ product.php?product_id=$1 [NC,L]
I've had that same problem on an older version of apache / mod_rewrite and I never found out what the exact problem was. But it definitely did not like rewriting /something/123 to /something.php?....
The quick fix I used, was renaming my php file to _something.php and using that.
So in your case renaming product.php to something that does not start with product like _product.php:
RewriteRule ^product/([0-9]+)$ _product.php?product_id=$1 [NC,L]
^ strangely enough this solved my problem...

Apache Rewrite URL Creating Linking Issues

Problem: rewrite url causing some links to break.
.htaccess has below rule:
RewriteRule ^blog/([0-9]+)/[-0-9a-zA-Z]+$ index.php?action=blog&postID=$1\%23disqus_thread [NC]
Style sheet reference in header template:
<link rel="stylesheet" type="text/css" href="style.css" />
I can click on:
domain.com/blog/1/title-of-article and get to the file just fine, but style sheet link breaks
If I go directly to:
domain.com/index.php?action=blog&postID=1#.UYV1mcqRiSo then the style sheet loads fine (ignore #.UYV1mcqRiSo, that is code from Disqus).
This is also breaking my logo link, which is:
<a href="./">
Instead of taking me to domain.com, it's going to domain.com/blog/1/
My basic file structure is:
index.php and style.css is in root, which loads up viewPost.php in/templates folder.
What is going and how do I correct this?
1. About the stylesheet link
You have to include these conditions in your .htaccess before the rewrite rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
The first means that rewrite rules work only if the requested URL is not to an existing file, and the second does the same for existing directories. Without these conditions the request for style.css is also passed to your index.php file, not to the style.css file directly.
If you have these rules already and it still doesn't work then the problem is with URL paths, see below.
2. About URL paths
You're using relative URLs in your links, both in the <a href="./"> and in the <link> tag. In this case they are resolved to the current directory of the URL (it doesn't matter that it's rewritten to the same file serverside, the path is prepared on the client side and every part separated with a slash / is treated as a directory. Thus if the current URL is domain.com/blog/1/, ./ is resolved to domain.com/blog/1/.) The path to style.css may be resolved to domain.com/blog/1/style.css. If you want these links work as if they're directly after the domain name in the URL, you have to use absolute URLs, i.e., / without the dot for the link on your logo; /style.css for the stylesheet link.
Easiest solution: Just set your links relative to the domain root, by fronting them with a slash (resp. removing the dot referring to the current folder in the link):
<link rel="stylesheet" type="text/css" href="/style.css" />
Logo

Strange php behavior

I was busy on my website doing URL rewrites when suddenly something happened from which I really don't know how it happened or how this is possible and how to maybe prohibit this from happening, probably some little mistake in the URL rewrite rule where I try to force the www in front of the address but when I remove the www. and press enter it comes up with an address which he can't find cause he doesn't put a / behind the .nl
somehow a extra index.php got fixed in between and what happens that in this way the whole website can be seen without any CSS markup...
it is reproducible, just fit the extra index.php in between like the example and the whole website can be seen and surfed without any markup from the CSS file
[http://www.capoeiravelsen.nl/index.php/index.php?page=home]
what is actually happening here ? why isn't it reading the CSS file in this way. of course users are not just gonna fix the extra index.php in between but does this presents a sort of security breach or bug or whatever....
[EDIT]
yeah it sounds logic that it can't fetch the CSS cause it looking in a
different directory or something, still don't know how it still can
fetch all the other docs then since a folder named index.php doesn't
exist in any way.
it happened with this rewrite rule:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} (.*)
RewriteRule (.*) http://www.%1$1 [R=301,L]
where I forced the www in the address, but when a some random crazy
user would want to delete the www. and pressed enter the weird effect
occurs.
the request would then change to:
[http://www.capoeiravelsen.nlindex.php/?page=home] where the browser
says it can't find that page of course
not noticing the / in between .php and ? I put a / in between .nl
and index.php so it would become
[http://www.capoeiravelsen.nl/index.php/?page=home] and pressed enter
then the whole address changed to:
[http://www.capoeiravelsen.nl/index.php/index.php?page=home]
where browsing the site is possible but without markup...
I solved this issue by changing the rewrite rule to:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
when some random user would delete the www. and pressed enter nothing
crazy happens the www. just gets forced again
You are linking the CSS relatively
<link href="inc/stylesheet.css" rel="stylesheet" type="text/css">
When you are accessing /index.php/index.php?page=home the browser look for the CSS file in
/index.php/inc/stylesheet.css
The Solution is :
Fix the rewrite so it won't allow using "/" after the php file (this way the OS looks for folder named "index.php"
link the CSS file absolutly:
i.e:
[link href="/inc/stylesheet.css" rel="stylesheet" type="text/css" />
(replace the [ with < :) )
Hope it helps
It will try to fetch the CSS files from the directory /index.php/ on your web server and not / as expected. It is not a security breach
I don't know about your rewriting rules since you haven't posted them, but the problem with missing CSS is because you're linking the stylesheet and other files in your HTML with relative paths, i.e.:
<link href="inc/stylesheet.css" rel="stylesheet" type="text/css">
Since your URL is http://www.capoeiravelsen.nl/index.php/index.php?page=home that makes the browser request a file located under http://www.capoeiravelsen.nl/index.php/inc/stylesheet.css, which, of course, doesn't exist on the server.
One solution is to change all paths in your HTML to absolute, i.e.:
<link href="http://www.capoeiravelsen.nl/inc/stylesheet.css" rel="stylesheet" type="text/css">

Mod Rewrite path issues

I have successfully created rewrite rules to handle URLs like
http://example.com/xyz
http://example.com/xyz/
http://example.com/xyz/abc
http://example.com/xyz/abc/
Here is the mod rewrite & regex:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([0-9]+)(?:\/)?$ /index.php?p=$1 [L]
RewriteRule ^/?([-a-zA-Z0-9_+]+)(?:\/)?$ /index.php?n=$1 [L]
RewriteRule ^/?([-a-zA-Z0-9_+]+)/([-a-zA-Z0-9_+]+)(?:\/)?$ /index.php?n=$2 [L]
This works great, but the problem is that when I go to http://example.com/xyz/abc, my script gets the correct input (n=abc) but the paths are messed up, so all relative URLs in my code are broken, as they are now relative to xyz instead of the root.
Is there a way around this at all? As you can see above I have tried to redirect to /index.php to force the path to be correct. My brain is fried after a long day of regex and code, so I hope it's not something disastrously trivial.
Use the HTML base element to force all relative URLs to use a different path than the one the browser displays.
<base href="http://www.example.com/"/>
...
link
The relative URL will use "http://www.example.com" as its base, even if the browser thinks it's looking at the page "http://www.example.com/xyz/". So the link goes to "http://www.example.com/relative/url" instead of "http://www.example.com/xyz/relative/url"
And there's the Stack Overflow way of doing it, in which every URL is either an absolute path (to resources like images) or paths including the root (i.e. "/feeds/questions/123"), which avoid the relative path issues.
The three options are:
1) Use a base tag (this will affect every relative URI on your page, including links, images, scripts, stylesheets and so on)
<base href="http://yoursite/">
2) Change all of your links to fully qualified URIs
<a href="http://yoursite/yourpage.html">
3) Use the "/" prefix to show that the path is relative to the root on each URI.
<a href="/yourpage.html">
I have personally used the base-tag option the most, which does get some bad press (from people that have used it without really understanding it). When it comes to mod_rewrite, the base tag is perfect as you probably DO want all your paths to be relative to the root, including all your images, css, scripts and links.
Edit: I assume you are talking about URLs in your HTML code, e.g. to images and stylesheets, that are broken.
Nope, as far as I know there is no way around it, because the browser sees a path, and requests resources relative to it. It has nothing to do with the server, and there is nothing you can do.
You will either have to resort to a different splitter that is not interpreted as a directory splitter (e.g. underscores), or use absolute paths, or use the <base> tag. I have never used the base tag myself, however, and it is not very well regarded wherever you look. The best thing would probably be to switch to absolute paths.
modify your last RewriteRule to include the path. you have the path in the regular expression.
RewriteRule ^/?([-a-zA-Z0-9_+]+)/([-a-zA-Z0-9_+]+)(?:\/)?$ $1/index.php?n=$2 [L]
or alternatively (depending on what you are trying to achieve):
RewriteRule ^/?([-a-zA-Z0-9_+]+)/([-a-zA-Z0-9_+]+)(?:\/)?$ /index.php?n=$1/$2 [L]
I tried using the BASE element in my pages as a shortcut instead of changing all urls. Add the base element as follows:
<base href="/">
And here are the results:
This: <link type="text/css" rel="stylesheet" href="my.css">
Becomes: <link type="text/css" rel="stylesheet" href="/my.css">
This: <script type="text/javascript" language="javascript" src="include/my.js"></script>
Becomes: <script type="text/javascript" language="javascript" src="/include/my.js"></script>
This: <a href="foo.html">
Becomes: <a href="/foo.html">
This: <a href="foo/bar.html">
Becomes: <a href="/foo/bar.html">
You can always override the base tag where necessary:
This: <a href="/foo">
Remains: <a href="/foo">
This: <a href="/foo/bar/">
Remains: <a href="/foo/bar/">

Categories