mod_rewrite issues and php - php

I'm trying to convert my app links, so that a link like this:
http://localhost/index.php?id=13&category=Uncategorized&title=just-a-link
gets converted to this:
http://localhost/13/Uncategorized/just-a-test
so far I was able to do it using:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /index.php?id=$1&category=$2&title=$3 [QSA,L]
but that completely breaks links to css and other files as it redirects every request with query to index.php
so I changed it slightly so that it only runs when the first query is a number:
RewriteEngine On
RewriteRule ^([0-9]*)/([^/]*)/([^/]*)$ /index.php?id=$1&category=$2&title=$3 [QSA,L]
this one doesn't break css and js files, however when you go to the link for example http://localhost/13/cat/test then try to go to another link like http://localhost/19/Uncategorized/something by clicking on it inside the previous page it will instead redirect you to something like http://localhost/13/Uncategorized/19/Uncategorized/just-a-test
How do I achieve what I described without any of these weird side effects??

Use :
RewriteEngine On
RewriteRule ^([0-9]+)/([^/.]+)/([^/.]+)/?$ /index.php?id=$1&category=$2&title=$3 [QSA,L]
And add this code to your html <header>:
<base href="/">
OR
<base href="http://www.domain.com/">
To fix relative css and js links

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

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

mod_rewrite RewriteRule and html links

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]

RewriteRule - html broken - Like missing css

I have problem with this rule:
RewriteRule ^([^/]+)/(\d+)$ more.php?books=$1&tags=$2 [L]
After this rule everything works but page is displayed incorrectly. Like missing css.
Thanks in advance
This might happen, when you have a relative URL to your CSS files. If you access the URL /book-name/2 and have a link to css/style.css, for example, the browser will resolve this to /book-name/css/style.css.
Change your CSS links to absolute URLs, like /css/style.css, and you should be fine.

PHP/Htaccess - problem with a loader

I have an htaccess file in the root that redirects every request of a page to a specific file:
RewriteCond %{REQUEST_URI} !^/loader.php(.*)?$ [NC]
RewriteRule ^(.*)$ /loader.php?url=$1 [QSA,L]
Now the redirect is easy in the loader.php
include($_SERVER['DOCUMENT_ROOT'] . '/' . $_GET['url']);
I just include the URL,admin/index.php for example.
If I leave the code as is the loader will include the file and print the correct HTML, but it will not load any CSS or JS scripts. If I put echo "test";
just before the include, the loader will load the CSS file. It's something that is killing me. Do I have to specify something in the HTTP header?
I already tried putting <base url="" /> in the header of index.php with no result,
but another strange thing is that with Chrom if I inspect the page and click on the link I'll see the right CSS.
Update 1
I printed the headers_list();. I noticed one thing - when I print an echo in headers_list, an array shows the content-type, so I tried to add it on my own with the header() function but with no result. Still working on it.
Update 2
I've noticed another thing; if I put a <style></style> tag with some CSS it will work fine, but if I use the <link /> tag it doesn't. This doesn't make any sense.
You have to send the correct MIME headers for css and javascript (and images etc.).
Easiest is to just let apache handle those requests. Put all the CSS, JS and images in a folder named 'assets' or something and change the htaccess to
RewriteCond %{REQUEST_URI} !^/assets/
RewriteCond %{REQUEST_URI} !^/loader.php(.*)?$ [NC]
RewriteRule ^(.*)$ /loader.php?url=$1 [QSA,L]

Categories