mod_rewrite RewriteRule and html links - php

I'm creating a new website and it has some php in it. The site basically would work like this, i have /index.php?page=category_page. The the category would be the category and the page would be the sort-of sub category / actual page. The rewritten rule would look like this: /category/page.
I've got this:
RewriteEngine On
RewriteRule ^category/([^/]*)$ index.php?page=$1 [L]
But i dont know how to separate the category and the page, any help?
The second thing is, in my index.php (for instance) i have some css from an external file:
<link rel="stylesheet" type="text/css" href="style/style.css"/>
This doesn't work with the rewrite rule, because it tries to load /category/page/style/style.css which doesn't exist / doesn't rewrite. How do i make it work? I know a simple fix would be to put /style/style.css and that would load from the root of the website, but i'm currently running the website from a sub directory e.g. example.com/new/index.php so that isn't an option. Any help with this?

Problem 1:
You could define multiple parameters in your regex, e.g. category & page (see below).
With such a broad rewrite rule, you would want to add a condition not to rewrite for stylesheets, images, and other assets, though.
I also modified your pattern to only match letters, digits, hyphens, and underscores, which would prevent the use of non-standard characters in your category or page names.
RewriteEngine On
RewriteCond %{REQUEST_URI} !\.(js|css|png|gif|jpg)$ [NC]
RewriteRule ^([\w\d\-]+)/([\w\d\-]+)/?$ index.php?category=$1&page=$2 [L]
Problem 2:
You're using a relative url in your href, which is appending the stylesheet's location to the current location defined in your browser (/category/page/).
Even though the server-side is rewriting that URL, the browser is unaware of the rewrite.
If you use an absolute URL instead, your browser will define the URL relative to the BASE url (/).
Try this:
<link rel="stylesheet" type="text/css" href="/style/style.css"/>

Use this RewriteRule
RewriteEngine On
RewriteRule ^(.+)/(.+)/$ index.php?page=$1_$2 [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...

Problems with Apache mod_rewrite

I decided to use mod_rewrite to make my URLs look better. I created very simple rules:
RewriteEngine on
RewriteBase /
RewriteRule ^(profile|contact|help|events|account|logout)/?$ index.php?p=$1 [NC]
RewriteRule ^home/?$ index.php [NC]
RewriteRule ^PlaceOrder/([0-9]+)/?$ index.php?p=mos&gc=$1 [NC]
It almost works well but it doesn't. So for example, [NC] is supposed to make it case-insensitive. However, for some reason, when I type for example localhost/Help in the browser, for some strange reason, it redirects to home page.
Another issue is with the last rule. If I type in localhost/PlaceOrder/1 it works as expected and opens index?p=mos&gc=1. But after that if I click on, for example, Account button, the browser uses this URL: localhost/PlaceOrder/account. Which is wrong. It should only use localhost/account. Why is it adding that sub-directory in there? It is only happening after the last rule is used. Prior to using the last rule, all links work well.
I'm going to guess that the localhost/Help isn't because of the rules and because of something in your index.php script. There's nothing those rules do that could possibly rewrite Help to the home page.
The second issue is a matter of a relative vs absolute URL issue. You're probably using relative links in your pages and because you've changed your relative URL base (which would be /PlaceOrder/ all relative links will have that prepended to it. You need to either change all your links to absolute URLs (they'd start with a /) or add this to your page's header:
<base href="/" />

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">

.htaccess rewrite rule breaking css

im still new to .htaccess
so i looked on stackoverflow for my answer and i cant find it. i have rules for my site i had to setup so i got my rules from this:
SEO Friendly URL to Dynamic URL using PHP
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?request=$1 [QSA,L]
but for some reason my css is broken but my php is good
example url:
http://www.domain.com/aboutme
http://www.domain.com/css/version.css
The problem is due to Capitalized file name Version.css.
If your devbox is windows it'll work. Windows is case insensitive.
But when you test it in server (Unix) it will break.
Because Unix is case sensitive
Better rename Version.css to version.css. Its safe for the future
I have a super answer for this problem. I've been looking everywhere for an easy solution that wouldn't make me hardcode baselinks for all my relative paths, like imgs, css, javascript...
So here it goes, add this between the <head> tags of the pages you are having problems:
<base href='http://www.mydomain.com/'>
This will make that your relative path links will start with this base link. Simple as that.
The <base> tag specifies the base URL/target for all relative URLs in a document. Put the <base> tag as the first element inside the <head> element, so that other elements in the head section uses the information from the <base> element.
Did it work for you?

Categories