Apache Rewrite module, complex rules - .htaccess

I basically want to redirect all requests to index.php doesn't matter what, except those with certain REQUEST_URI. Those requests that look like image files, so have an ending like: .jpg or .png should be examined and if they are under the public/ folder (or any subfolders in any depth) and if they are they should be served and the rewriting process should stop here! If not, I want to redirect to a default image at public/errors/image-not-found.png and terminate rewriting process. The other exceptions are files that end with .js, .css, .html or .swf. They also should only be served if they are located under the public/ folder or any other subfolders. If not, a simple 404-Not found should be sent back. In either case of the last to the rewriting process need to stop of course.
Any other request should be redirected to index.php and appended as a query string. (even if the request points to a directory or to a file that is not under the conditions aforesaid, but exists, e.g: www.xyz.com/library/Database.php -> www.xyz.com/index.php?url=library/Database.php)
I have half-measure solution:This is how I redirect everything to index.php:
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
I append a visual explanation of what I want. Maybe this is clearer:

Basically, you don't want to do anything if the requested file exists in public/ or any of its subfolders. So, first we deal with those:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/public/.*\.(html|css|js|swf|jpe?g|png|gif|bmp|ico)$ [NC]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
Now, that is over with. We now check whether an image was requested:
RewriteCond %{REQUEST_URI} \.(jpe?g|png|gif|bmp|ico)$ [NC]
RewriteRule ^ /public/errors/image-not-found.png [R,L]
Similarly for other static files:
RewriteCond %{REQUEST_URI} \.(html|css|js|swf)$ [NC]
RewriteRule ^ - [R=404,L]
Redirect everything now to index.php:
RewriteCond %{REQUEST_URI} !/index\.php$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteRule ^.*$ /index.php?url=$0 [R,L]

Following series of rules should probably mimic the flow-chart:
# for public folder pass through
RewriteRule ^public/.+?\.(?:jpe?g|ico|png|bmp|css|js|html|swf)$ - [L,NC]
# for other images
RewriteRule ^.+?\.(?:jpe?g|ico|png|bmp)$ /public/errors/img-not-found.jpg [L,NC,R=302]
# for other css|js|html|swf URIs
RewriteRule ^.+?\.(?:css|js|html|swf)$ - [L,NC,R=404]
# everything else, route to index.php
RewriteRule ^((?!index\.php).+)$ index.php?url=$1 [NC,QSA,L]

Related

htaccess of front controller seems to block images, JavaScript, and CSS

I have a problem regarding my .htaccess file. It is placed in the root folder of my site together with the index.php (my front controller) and the folders regarding CSS, JavaScript, and images. The following is the URL-related content of my .htaccess file
# skip existent files
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule index.php - [QSA,L,C]
RewriteRule .* - [QSA,L]
# protect PHP files from the outside
RewriteCond %{REQUEST_URI} ^.*\.php$
RewriteRule ^.*\.php$ - [R=404,L]
RewriteCond %{REQUEST_URI} ^img/*$
RewriteRule ^img/*$ - [QSA,L]
# refer root to index.php
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^$ index.php [QSA,L]
# redirect 404 for non existent files
RewriteCond %{REQUEST_URI} ^(.*)\..*$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)\..*$ - [R=404,L]
# adjust the rest of the domains
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?site=$1 [QSA,L]
When I then open the website in the browser, it does not load the respective resources. If I click on "show image source", it immediately refers to the 404 page.
Do you have any idea which part I need to adjust to work around this problem? The root structure looks as follows
root
-.htaccess
-/css
-/js
-/img
-index.php
Thank you very much in advance! :)
EDIT 10/08/2021:
I figured out that the problem was stemming from other RewriteRules which I put into the last section right before RewriteRule ^(.*)$ index.php?site=$1 [QSA,L], e.g.:
RewriteRule ^study-programmes/(.*)$ index.php?site=study-programmes&faculty=$1 [L]
However, I have several pages where I need specifically named parameter (such as faculty in the example above). What exactly do I need to adjust in order to make it work?
The image matching rules are not written correctly. The . is missing from the rule.
RewriteCond %{REQUEST_URI} ^img/.*$
RewriteRule ^img/.*$ - [QSA,L]

Folders not found after mod_rewrite

I have the following project structure:
There is a folder called 'views' containing all my HTML files.
The problem now is, that my url looks like this: http://example.com/views/index, http://example.com/views/account,...
How do I use mod_rewrite in my .htaccess to get rid of the views part in the URL?
I just want that when someone visits http://example.com, it actually sees the file http://example.com/views/index.php.
I've got very close with this:
RewriteCond %{REQUEST_URI} !^/(views)
RewriteRule (.*) /views/$1
But the problem is that now all my other folders are not found anymore. My CSS from the folder css ain't found, also my media files are not found, and also not my PHP.
Edit:
This is my new .htaccess:
# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php
RewriteRule ^(css|js|media|partials|php)($|/) - [L]
RewriteCond %{REQUEST_URI} !^/(views)
RewriteRule (.*) /views/$1
It gets very close. On the third line from the bottom I was able to exclude all folders for the rewrite_rule, resulting in media, css, js,... loading. But the problem is that that line, also removes the rewrite_rule above.
In the folder php there are files like example.php, test.php,... But I refer to it without extension. But that's not working anymore because the third rule from the bottom ignores ALL rewrite_rules; so also the rules removing the PHP-extension.
So how do I exclude folders for only a specific rewrite_rule?
Edit2:
Solved.
Correct .htaccess:
RewriteEngine On
# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php
RewriteCond %{REQUEST_URI} !^/(views|css|js|media|partials|php)
RewriteRule (.*) /views/$1
Your current rule adds prefix /views to all URLs, including those starting with /css, /js, /media and so on. You need to exclude those.

Rewrite rule in htaccess is not working

Here are the .htaccess rules:
RewriteEngine On
RewriteRule ^([^/]*)$ content.php?seourl=$1 [L]
RewriteRule ^pdf/([^/]*)$ content-single.php?seourl=$1 [L]
RewriteRule ^browsepdf$ browse.php [L]
RewriteRule ^browsepdf-([^/]*)$ browse.php?page=$1 [L]
RewriteRule ^download/([^/]*)$ download.php?pdf=$1 [L]
RewriteRule ^sitemap.xml$ xmlsitemap.php [L]
Options -Indexes
Here every URL is pointing to content.php?seourl=$1, even css, js and image files.
Here are some example URLs I need,
http://example.com/sjskjfsk21
http://example.com/asfasfasf43sf
http://example.com/pdf/fhfdhdh3432aaf
http://example.com/pdf/aisfyiahm2faf3
http://example.com/browsepdf
http://example.com/browsepdf-1
http://example.com/browsepdf-2
http://example.com/download/fjaskfjalsk3rs
http://example.com/download/usaydiy7aisydi
http://example.com/sitemap.xml
Can anyone please fix the .htaccess file.
Here every url is pointing to "content.php?seourl=$1"
Because your first (generic) rule catches all the requests. You need to change the order so you have the most specific rules first, and the most generic (catch-all) rules at the end. In your case you just need to move the first rule to the end. For example:
RewriteRule ^pdf/([^/]*)$ content-single.php?seourl=$1 [L]
RewriteRule ^browsepdf$ browse.php [L]
RewriteRule ^browsepdf-([^/]*)$ browse.php?page=$1 [L]
RewriteRule ^download/([^/]*)$ download.php?pdf=$1 [L]
RewriteRule ^sitemap\.xml$ xmlsitemap.php [L]
RewriteRule ^([^/]*)$ content.php?seourl=$1 [L]
NB: I backslash-escaped the dot in sitemap.xml to match a literal dot, otherwise it matches any character.
even css, js and image files.
You can make an exception for these static resources at the beginning of your file, before the existing directives. For example:
RewriteRule \.(js|css|png|jpg|gif)$ - [L]
For any URL that ends in any of the stated file extensions then stop processing the current mod_rewrite rules.
Alternatively (although perhaps marginally less efficient), you can prevent processing of requests for files that exist. Again, this goes before your existing mod_rewrite directives. For example:
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
However, this must now check every request for the existence of a file on the filesystem that maps to the request. (It could also be combined with the above rule if required.)
UPDATE: Bringing this together, we have:
# Exclude any existing files from being rewritten
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# Specific rewrites
RewriteRule ^pdf/([^/]*)$ content-single.php?seourl=$1 [L]
RewriteRule ^browsepdf$ browse.php [L]
RewriteRule ^browsepdf-([^/]*)$ browse.php?page=$1 [L]
RewriteRule ^download/([^/]*)$ download.php?pdf=$1 [L]
RewriteRule ^sitemap\.xml$ xmlsitemap.php [L]
# Any other requests for the form "/<anything>"
RewriteRule ^([^/]*)$ content.php?seourl=$1 [L]

How to make RewriteRule work only if URL contains specific string?

I need to add RewriteRule which works only if there is a certain string in the URL, otherwise I need to load the content from another folder. If the URL contains the string test, I need to send it to index.php as a parameter, else the content should be loaded from the directory folder.
For example: The root folder of the project is new_project. If the URL is http://localhost/new_project/test/something/, then I need to send test/something/ to index.php as parameter. Else if the URL is something like http://localhost/new_project/something/, then I need to load the content from directory/something folder.
Following is the .htaccess file I've written so far:
Options +FollowSymLinks
RewriteEngine on
# Force Trailing Slash
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
# Send request via index.php (again, not if its a real file or folder)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
# Serve other requests from the directory/ folder
RewriteRule ^(.*)$ directory/$1 [L]
What needs to be changed in the above .htaccess file so that the occurrence of test string in the URL passes the string after the string test along with the string test to index.php and if the URL doesn't contain the string test then loads the content from the directory folder?
You can test for the test/ inside the RewriteRule itself. Place the more specific rewrite rule before the "catch-all" directory/ rewrite rule.
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
# All requests starting with `test/` go to index (without the `test` part).
RewriteRule ^test/(.*)$ index.php?/$1 [L] # Not sure how GET parameters starting with `?/` behave.
# All others go to directory. Assuming not a valid file or dir.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ directory/$1 [L]

Logical AND in htaccess modrewrite?

This is my htaccess file
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{REQUEST_URI} !^/chat/
RewriteCond %{REQUEST_URI} !^/m/
RewriteCond %{REQUEST_URI} !^/__admin/
RewriteCond %{REQUEST_URI} !^/gzip_headers.php
RewriteCond %{REQUEST_URI} !^/phpfreechat/
RewriteCond %{REQUEST_URI} !^/_temp/
RewriteRule ^.+\.php$ index.php [L]
RewriteRule ^.*\.css gzip_headers.php [L]
RewriteRule ^.*\.js gzip_headers.php [L]
RewriteRule ^classifieds/ /index.php [L]
RewriteCond %{REQUEST_URI} !^/movies/.
RewriteRule ^movies/ /index.php [L]
RewriteCond %{REQUEST_URI} !^/games/.
RewriteRule ^games/ /index.php [L]
RewriteRule ^jntu/ /index.php [L]
RewriteRule ^news/ /index.php [L]
My idea behind this basically is,
forward everything to public_html/index.php (except some directories)
forward all js and css to gzip file, ( i am doing this basically because im not jsut gzipping them but also compressing in tha phpfile)
the problem is when I load images from subdirectories the are redirected to index.php as well, so just creating conditions for those directories and storing images in them like RewriteCond %{REQUEST_URI} !^/games/.
I would like to make it simple to do stuff like this
forward everything to index.php (except some conditions on top)
forward css and js to gzip file
load images and flash and some other mime types straight away only if they exists. (jpg|gif|png|swf|flv|mp4|3gp|mp3|zip|rar|exe)
Something like logical AND REQUEST_URI and -f flag I guess
Try these rules:
RewriteCond %{HTTP_HOST} =example.com [NC]
RewriteRule ^ http://www.example.com%{REQUEST_URI} [L,R=301]
RewriteRule .*\.(js|css)$ gzip_headers.php [L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .+\.(jpg|gif|png|swf|flv|mp4|3gp|mp3|zip|rar|exe)$ - [L]
RewriteCond %{REQUEST_URI} !^/(gzip_headers|index)\.php$
RewriteCond %{REQUEST_URI} !^/(chat|m|__admin|phpfreechat|_temp)/
RewriteRule ^.+\.php$ index.php [L]
I'm not sure why your images are being redirected if your rule only redirects URIs ending with '.php'. That should exclude all other file extensions from the rule.
I'm also not sure what you mean by needing 'logical and'. When you have a number of RewriteCond lines before a RewriteRule those conditions are ANDed together and the rule is only applied if they all are true.
You can't use modrewrite to check for the existance of files and say "if the file exists, don't apply any rules, just serve up the file".
I think the best solution would be to either use a single top-level directory called 'static' or 'images' where you put all your files and exclude it from the rules, or have a wider-matching rule.
So for example you could make 'static' or 'images' a special directory name and exclude any url that contains .*/images/.* from the rules. Then /something/images/image.jpg and /something/else/images/image.jpg would both be excluded and the file would be served up.
Another hacky way would be to serve the files up from PHP. So in PHP you would translate $_SERVER['REQUEST_URI'] into a filename and see if it exists. If it does, you can write the file contents to the PHP output stream, although this won't be as efficient as leaving it up to Apache, and actually I really would not recommend it.
But like I said before, if your rule is only matching files that end with .php then your images should not be getting redirected. I would figure out why this is happening first. There is a way to turn on debug logging for mod_rewrite but you'll have to Google that.

Resources