.htacces css/js subdirectory issue - php

I am trying to make some changes to .htaccess file of a site. I have searched all over the web, but I cant find a clear solution for that, or there is possibility that I have got it wrong. So here we are are:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/$ /e/www/ [R]
RewriteRule ^news$ /news.php
RewriteRule ^news$ /news.php
RewriteRule ^resources$ /resources.php
RewriteRule ^lexicon$ /lexicon.php
RewriteRule ^contacts$ /contacts.php
RewriteRule ^analytics$ /analytics.php
RewriteRule ^([0-9]+)-([0-9]+)-([0-9]+)-(.*)$ /news.php?yy=$1&mm=$2&dd=$3&alias=$4
RewriteRule ^resources$ /contacts.php
RewriteRule ^articles_search$ /articles_search.php
RewriteRule ^articles_data_search$ /articles_data_search.php
RewriteRule ^profile/([0-9]+)&commit=(.*)$ /profile.php?id=$1&commit=$2
RewriteRule ^profile/(.*)&commit=(.*)$ /profile.php?alias=$1&commit=$2
At last 2 rows, i am trying to have a result from this:
www.example.com/profile.php?alias=kokazani&commit=Edit
to this:
www.example.com/profile/kokazani&commit=Edit
The problem now is the wrong path of css and js files. I need a solution, or a better approach of rewriting rules. I think that a solution ,maybe, is to rewrite everything like css/blabla.css to root directory/css/blabla.css, but if there a standard-solution foro this problem, i would like to know :)
Thanks in advance.

There are 2 possible (simple) solutions:
Use absolute paths for your CSS files (/include/css/style.css etc)
Use HTML's <base> element to set the correct base URL for relative URLs to follow (rather then the directory of the document which is altered by Apache).

My approach would be (as suggested by Truth) using absolute paths. If this is not possible, you could use:
RewriteRule ^(.+)/css/(.*)$ /css/$2
But be warned: This means the client (browser) will end up getting and caching multiple copies of the same file because it is fetching it (as far as it can see) from different paths. Therefore you lose the advantage of client-side stylesheet caching.

Related

fix paths with pretty urls?

I am trying to use pretty links with my website, but i have a problem with paths of css and js files.
Thats what i wrote in .htaccess file
RewriteEngine on
RewriteRule ^register register.php [NC,L]
RewriteRule ^login login.php [NC,L]
RewriteRule ^settings settings.php [NC,L]
RewriteRule ^logout logout.php [NC,L]
RewriteRule ^profile/([0-9a-zA-Z]+)$ profile.php?u=$1 [NC,L]
The problem here appears when i try to access profile.php with the variable
example: http://example.com/xxxx/profile/ashraf
This here will consider profile as the main folder when importing style files which is not a folder, it's just profile.php
Thank you
I think you can add this before your rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Basically what this ( common on many CMS systems ) says, is if Not a real file !-f, or a real directory !-d then continue on. Therefore if the file actually exists, such as a css file, it will not pass the condition and will not be re-written.
So I would say put it right here
RewriteEngine on
## insert here ##
RewriteRule ^register register.php [NC,L]
You may have to place it before each rule, that I am not that sure of, as I haven't done a whole lot with .htaccess in like 5 years. Basically when I learned how to use the URI instead of a URL and route everything through a index.php and a router script.
This is the extent of my .htaccess files these days ( just FYI for my explanation of my lack of remembering ) and it literally never changes. That's one of the biggest benefits of building a router and using the URI
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
This answer I did has a pretty good ( if I say so ) explanation of how what I call the URI method works.
How to change the name of the directory in url in php using htaccess?
Here is another answer I did on this topic that outlines how to build a basic router
Oop php front controller issue
Anyway hope that helps.
The browser cannot know if the paths in the URL are real directories or just parts used in rewrite rules. When it sends a request to the web server to get the CSS, the browser must convert it any relative urls to absolute and assumes the current page is in a path of directories.
In other words, in your HTML page, always use absolute urls: <link href="/xxxx/style/mycss.css" ...>
Put this after the profile.php rule. This will take out the extra 'profile'.
RewriteRule ^profile/(.*)$ $1 [L]
Or use a long path to the css file:
<link href="/xxxx/style/style.css" rel="stylesheet">
Either one should work.

Rewrite/hiding some URL in .htaccess

I'm trying to get right syntax for .htaccess without any result...
I've a URL structured as domain.com/app/public/pageName .
It's working fine but I would "hide" the 'app/public/' part in browsers, basically doing something like:
[real URL] domain.com/app/public/pageName -> domain.com/pageName [what users type and see in browsers]
I think in that way it should be more readable and seo-friendly.
As I understood from docs (and maybe it's wrong because it's not working...) I should tell to Apache to map/redirect all URL like domain.com/pageName to domain.com/app/public/pageName , but only internally, in order to show the minimal URL in users' browsers.
Right now I have something like:
RewriteEngine on
#RewriteBase /app/public/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ https://localhost/app/public/index.php?url=$1 [QSA,L]
(I'm using full URL with https://... in order to get something that will be quick and easy to adapt when I upload all to my hosting, is it right?).
Problem is that RewriteRule actually change the URL, because it perform a redirect and URL rewrite it's not handle internally.
So, first of all: is it possible what I'm trying to do? If so, how can I handle the URL rewrite only internally?
Everything should be uploaded to a shared hosting, so I don't have other than .htaccess.
Anyway, I can consider to upgrade to a vps if there are not other possibilities...
Thanks!
==============
EDIT (should be more clear now)
tl;dr version:
I'm looking for a method that let users to type domain.com/pageName (and they will see that address in their browsers) and rewrite internally that URL in order to point to domain.com/app/public/pageName.
==============
More: after /app/public/ there can be an arbitrary number of elements, separated by / . All of these elements are appended at the end of the URL after index.php. At the end URL looks like:
domain/app/public/index.php?url=lot/of/elements/here
This is already working with the RewriteRule posted above, I would keep that too.
Thanks!
This is working fine for me, Hope it will work for you as well.
Check .htaccess here
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} ^/app/public
RewriteRule ^app/public/(.*)$ /$1 [L,QSA]
Just for reference, I found a solution, maybe will be usefull for someone.
Basically I moved .htaccess to the root server (instead of /app/public directory) and changed the RewriteRule as follow:
RewriteEngine on
RewriteBase /app/public/
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [PT]
Now it's working (at least on localhost).
What do you think? Are there any side effects with this config?

.htaccess filename to url

I've been searching and cannot find an answer that suits my needs. It is a rather simple question I assume.
The point is that I need to rewrite my files to a name of my liking. For example I have, 'www.mydomain.com/account_index.php' which I want to rewrite to www.mydomain.com/account
Thus far I had this, but that doesn't seem to work with multiple lines.
Rewriteengine on
rewriterule ^support account_ticket.php [L]
rewriterule ^account account_index.php [L]
So in short, I want my website to redirect /account to account_index.php and the other way around.
Thanks in advance!
I found the answer for those that are wondering.
I had to put a RewriteCond before every RewriteRule.
So if I wanted to go to www.mydomain.com/account. I'd have this:
RewriteCond %{REQUEST_URI} ^/account$ [NC]
RewriteRule ^account account_index.php [NC,L]
This means that /account is now linked to account_index.php.
Have a nice day!

htaccess RewriteRule two scripts

Good day!
I have two scripts in my root folder:
/index.php
/home.php
Both of them receive get parameter and display different pages, f.ex:
index.php?page=info
home.php?page=about
1) When I try to do clean URI by this:
RewriteRule ^([a-zA-Z0-9]+)$ index.php?page=$1 #working ok
RewriteRule ^([a-zA-Z0-9]+)/$ index.php?page=$1 #doesn't load css,js
I found three advices: rather apply path of css,js folders to htaccess, use tag or use absolute path.
I'm wondering are there any other ways to make it solely by htaccess. Or is it possible to change query string the way to remove "/" if there is one f.ex:
I insert
example.org/info/
And my string automatically becomes
example.org/info
2) My htaccess only works with index.php. How should I change my rule to work with home.php if there is "userhome" in query string to make such queries possible:
example.org/home/about
P.S
RewriteRule ^home/([a-zA-Z0-9]+)$ home.php?page=$1
Working the way I need, concerning my second question. However I haven't still done with the problem of css, js loading. Any tips please
The best way to handle CSS not loading is to use <base> tag. That way you can use relative URL's and it won't break anything with your rules.
This will go in the head section of your site..
<base href="http://www.example.com/" />
You can try these rules for your your requirements.
#Redirect to home.php
RewriteRule ^home/([a-zA-Z0-9]+)$ home.php?page=$1 [L]
#Remove the trailing slash
RewriteRule ^([a-zA-Z0-9]+)/$ /$1 [R=301,L]
#Redirect to index.php
RewriteRule ^([a-zA-Z0-9]+)$ index.php?page=$1 [L]

Bug with mod_rewrite and PHP?

I've run into a bug and I'm not sure of its source. I use mod_rewrite for the following:
RewriteRule ^stuff$ index.php?page=stuff [L]
and it works just fine, but when I use
RewriteRule ^(.+?)$ index.php?page=$1 [L]
CSS is no longer applied to the page and the code doesn't seem to read GET requests.
Can someone elaborate on why this happens for me?
Add a rewrite condition to not rewite if the file is present & use the QSA flag to allow appended parameters:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]
Since your query strings have been converted into a directory-like structure, apache will check the directory if you are not using any path disclosure.
So if you're include the css file like so:
<link href="skins/style.css">
You'll need to disclose the full webpath to the file:
<link href="http://site.com/skins/style.css">`
Also, for all images in your css file you need to do the same, include the full webpath to avoid and issues when rewritting in the future.

Categories