.htaccess clean URL and relative paths - .htaccess

I have the following in my .htaccess which is :
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /index.php?type=cat&id=$1 [QSA,L]
RewriteRule ^page/([^/]+)$ /index.php?type=page&name=$1
This seems to work just fine but relative paths to pictures and css files inside of index.php become broken in the second case (Page). did not work. In second case, all images are pointing to page/images/ instead of image/
Other than hardcoding the actual path to images, is there any other way to fix this?
images, css, js folders are located in the root. This is how the root looks like
.htaccess
index.php
images/
css/
js/

RewriteCond directives only apply to the rule directly following them.Try the follwing
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /index.php?type=cat&id=$1 [QSA,L]
RewriteCond %{REQUEST_URI} !\.(css|js|jpe?g|gif|png)$ [NC]
RewriteRule ^page/([^/]+)$ /index.php?type=page&name=$1 [L]
#rewrite requests for page/images to images
RewriteCond %{REQUEST_URI} ^/page(/images/.+)$ [NC]
RewriteRule . %1 [L]
EDIT.
Modified to rewrite page/images to images

Related

I made a dynamic sub folder using .htaccess, but the browser can't find the images in the root folder

I made a dynamic folder this way:
RewriteRule ^user/(.+)$ index.php?user=$1 [QSA]
I want the browser to look in the root folder for images. Currently it looks for them in example.com/user
Could you please try following, its looking for if a uri is NOT ending with png format then redirect it to your index.php file, in case you have more formats like .jpg etc then change following regex from !\.png$ TO !(\.png|\.jpg)/?$.
RewriteEngine ON
##First rule to skip .png files to go to index.php.
RewriteCond %{REQUEST_URI} !\.png/?$ [NC]
RewriteRule ^(.*)$ index.php?user=$1 [NC,NE,L]
##Second rule to serve .png files from images folder.
RewriteCond %{REQUEST_URI} !images [NC]
RewriteCond %{THE_REQUEST} \.png/?\s [NC]
RewriteRule ^(.*)$ /images/$1 [NE,L]
The answer was to do this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.+)$ index.php?user=$1 [QSA]
Thanks for ideas, RavinderSingh13!

Mod-rewrite for css, js, html and directories in Apache/PHP

I am using mod_rewrite in .htaccess. I can't figure out how to get the following done.
All css pages should go to /minify/?f=/styles.css
All js pages should go to /minify/?f=/js.js
All .txt, .gif, .jpg, .png files should open as they are
All other pages (eg: /page, /dir/page, /dir1/dir2/page) should go to /page.php
All other files in directories should open as it is (eg: /admin/login.php)
Edit:
This is the code that I came up with. It's not elegant, and it fails sometimes. For eg: I can't get /admin/login.php to work. When I open that URL, it's going to page.php.
RewriteEngine on
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|\.txt)$
RewriteRule .* page.php
RewriteRule ^.*?\.css$ /min/?f=/styles.css [L]
RewriteRule ^.*?\.js$ /min/?f=/js.js [L]
Try the following:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.(css|js|png|txt|gif|jpe?g)$
RewriteRule ^ /page.php [L]
RewriteRule \.css$ /minify/?f=/styles.css [NE,L]
RewriteRule \.(js)$ /minify/?f=/$1.$1 [NE,L]

.htaccess URL rewrite directory

so I want to rewrite a URL. The default URL is http://example.com/m.php?i=random_string
Here's the rule
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]
RewriteRule ^m/([^/]+)/?$ m?i=$1 [L]
However, when I access http://example.com/m/random_string, the page comes up but my external css and js files don't load. Why does this happen? I thought that the directory does not change from the original rewritten URL.
The directory indeed does not change, but browser doesn't know that - what it gets looks like a directory, such as m/something, so it tries to load the CSS file relative to this directory.
To fix that, use absolute paths to your CSS and JS in the page, ie.:
/css/style.css
(the initial forward slash makes it absolute)
RewriteEngine On
RewriteRule ^m/([^/]+)/?$ m.php?i=$1 [L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)(\?.*|)?$ %{REQUEST_FILENAME}.php$2 [QSA,L]
Have your rules like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^m/([^/]+)/?$ m.php?i=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d and RewriteCond %{REQUEST_FILENAME} !-f will prevent first rewrite rule to be applied for real files and directories.

Codeigniter index.php breaking image paths

When I don't edit my .htaccess file, my image path reads something like: http://this.website.com/codeigniter/inc/images/logo.jpg.
However, as soon as I add this code into the .htaccess file:
RewriteEngine On
RewriteCond $1 !^(index\.php)
RewriteRule ^(.+)$ index.php?$1 [L]
(to remove the index.php from the URL), I get 404 error, even with the exact same path.
How do I remove index.php and still have access to my images?
RewriteEngine On
RewriteCond $1 !^index\.php [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?$1 [L]
The two new RewriteConditions would exclude existing directories and files (and so images) from redirection.

How to remove extension and force trailing slash with .htaccess

I managed to solve this problem by the following code:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)/$ $1.php [L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ $0/ [L,R=301]
In other words, not to do anything if its a directory.
Yet, my current problem is that css and the images are not loaded until I change the path to the css file and to the images to the absolute path.
Is there any other way to solve it rather then changing all the paths in all the files in the website to absolute.
Thanks a lot.
Add RewriteCond %{REQUEST_FILENAME} !-f to the first two RewriteCond, then images and css won't hit the rewrite, but PHP files will, and things that are not folders will also hit the rewrite

Resources