.htaccess rewrite everything except images, scripts, and stylesheets - regex

I have a web project with two directories: "core" and "public".
"Core" contains all of the controllers, views, and files required for the Model-View-Controller.
"Public" contains all public files, like js, css, and less, that can be accessed directly.
I have the following .htaccess in the main directory:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !(\.png|\.jpg|\.gif|\.jpeg|\.bmp|\.css|\.js|\.less|\.coffee)$
RewriteRule ^$ core/ [L]
RewriteRule (.*) core/$1 [L]
</IfModule>
However, it still rewrites those files to the 'core' directory.
What am I doing wrong?

I recommend you use this instead:
RewriteEngine on
# First, check if a specific type is being requested
RewriteCond %{REQUEST_URI} !\.(png|jpg|gif|jpeg|bmp|css|js|less|coffee)$ [NC]
# Second, check if the request is for an existing file
RewriteCond %{REQUEST_FILENAME} -f
# If both conditions are true, then skip rewriting
RewriteRule ^ - [L]
# Otherwise, continue:
RewriteRule ^$ core/ [L]
RewriteRule (.+) core/$1 [L]
The reason your assets were being sent to core is because the conditions only work for the first rule, which was for your application index. Using this method tells mod_rewrite to skip rewriting if an asset is being requested. Once it does that, it can continue with all other rules.

Related

.htaccess : include a subdirectory within URL (only for certain files)

I am trying to write a seemingly simple RewriteCond, however, I've been chasing my tail trying to find a solution. I have searched the docs (here, here), in addition to this similar query and this similar query, but haven't been able to solve the issue.
FOLDER STRUCTURE
/config
/public
/js
/css
index.php
/src
/controllers
/ajax
/models
/views
AIM
If the URL is too www.example.com, or any .js, .css, .jng files, include 'public' in the URL.
wwww.example.com -> www.example.com/public/index.php
www.example.com/js/main.js -> www.example.com/public/js/main.js
However, I wish to write a RewriteCond that exempts any other URL request.
wwww.example.com/src/controllers/ajax/doSomething.php -> NO CHANGE
Try this:
RewriteRule ^$ /public/index.php [L,R]
RewriteCond %{REQUEST_URI} \.js$ [OR]
RewriteCond %{REQUEST_URI} \.css$ [OR]
RewriteCond %{REQUEST_URI} \.jng$
RewriteCond %{REQUEST_URI} !^/public
RewriteRule ^ /public%{REQUEST_URI} [L,R]

Rewrite rules ignoring my dist folder

So I have a bunch of .htaccess rules for my angular application and I'm looking for the behavior:
If dist/frontend.html exists, route everything to that. If not route to frontend.html.
This is what I currently have. The rules work as long as I'm requesting a sub route (e.g. http://www.domain.com/404/) but if I'm requesting the root, with no path it's like these rules are never executed and it just relies on the DirectoryIndex.
DirectoryIndex index.html index.html frontend.html index.php
# Rewrite everything which looks like it should be handled by angular here, 404 if a specific resource
RewriteCond %{REQUEST_URI} !\.(jpg|png|jpeg|gif|css|js|html)$ [NC]
RewriteCond %{DOCUMENT_ROOT}/dist/frontend.html -f
RewriteRule ^ dist/frontend.html [L]
RewriteCond %{REQUEST_URI} !\.(jpg|png|jpeg|gif|css|js|html)$ [NC]
RewriteCond %{DOCUMENT_ROOT}/frontend.html -f
RewriteRule ^ frontend.html [L]
Problem is this condition:
RewriteCond %{REQUEST_URI} !\.(jpg|png|jpeg|gif|css|js|html)$ [NC]
Since you're ignoring .html extension and also using index.html as default handler using DirectoryIndex causing both of your rules to be skipped.
If you're going to use one of the frontend.html using one of the 2 rules then you don't really need DirectoryIndex directive, just comment it out and it should work.

What is the error in this .htaccess?

Using mod_rewrite, I want to look for all requested files in an assets directory, and otherwise fallback to index.php for API calls. This is my .htaccess.
RewriteEngine on
RewriteRule ^.+\.[^/]+$ assets/$1 [L]
RewriteRule .* index.php
Asset files are determined by whether the last last url segment has a file extension, which means a dot without following slashes. I already tested the regex at regexpal and it seems to work fine.
Unfortunately, all request result in a 500 Internal Server Error using these rewrite rules. I know that mod_rewrite is set up correctly, since omitting the second line works as expected.
Try these rules:
RewriteEngine on
# rewrite to assets/file if file exists in assets dir
RewriteCond %{DOCUMENT_ROOT}/assets/$1 -f
RewriteRule ^(.+?\.[^/]+)/?$ assets/$1 [L]
# otherwise rewrite to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^assets/ index.php [NC,L]
You need to add a condition to prevent the rewrite engine from looping:
RewriteEngine on
RewriteCond %{REQUEST_URI} !index\.php
RewriteRule ^.+\.[^/]+$ assets/$1 [L]
RewriteCond %{REQUEST_URI} !index\.php
RewriteRule .* index.php
The rewrite engine loops until the URI stops changing, and since .* matches index.php as well, it'll continue to loop through that rule until the internal recursion limit is reached you get a 500 error.
Additionally, you can add conditions that won't reroute existing files or directories:
RewriteEngine on
RewriteCond %{REQUEST_URI} !index\.php
RewriteRule ^.+\.[^/]+$ assets/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php

.htaccess redirection of index.php

The below illustrates the .htaccess code for a generating SEF url when joomla SEF is on in the backend.
## Begin - Joomla! core SEF Section.
#
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
#
# If the requested path and file is not /index.php and the request
# has not already been internally rewritten to the index.php script
RewriteCond %{REQUEST_URI} !^/index\.php
# and the request is for something within the component folder,
# or for the site root, or for an extensionless URL, or the
# requested URL ends with one of the listed extensions
RewriteCond %{REQUEST_URI} /component/|(/[^.]*|\.(php|html?|feed|pdf|vcf|raw))$ [NC]
# and the requested path and file doesn't directly match a physical file
RewriteCond %{REQUEST_FILENAME} !-f
# and the requested path and file doesn't directly match a physical folder
RewriteCond %{REQUEST_FILENAME} !-d
# internally rewrite the request to the index.php script
RewriteRule .* index.php [L]
I have a simple problem which I'm not able to get my head around into.
I put this condition so that if the url is domain.com/index.php then it should go to domain.com and this should happen only if the pattern matches exactly as index.php.
However this happens for all urls.
RewriteCond %{THE_REQUEST} [^.]*|/(index.php)$
RewriteRule ^index.php$ / [R=301,L]
it should go to domain.com and this should happen only if the pattern matches exactly as index.php. However this happens for all urls.
It is due to the wrong regex you're using. Use this rule to remove index.php from URIs:
RewriteCond %{REQUEST_URI} !/administrator [NC]
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php$ /$1 [L,R=302,NC,NE]

Apache .htacces Rewrite Rule to Remove .php File Extensions

The title explains the basics, now, here comes the problem:
I have many files that look like this:
<?php include 'mynavbar.php';
include 'myheader.php';
include 'myfirstline.php';
include 'mybuttons.php';
include 'myadvert.php';
include 'myimg.php';?>
And I can't edit all the files and remove the .php. How can I not show the .php in the address bar but still be able to include the pages by using the .php ending
Following code should work for you in .htaccess file to hide .php extension:
Options +FollowSymlinks -MultiViews
RewriteEngine on
# to make `/path/index.php` to /path/
RewriteCond %{THE_REQUEST} ^GET\s(.*/)index\.php [NC]
RewriteRule . %1 [NE,R=301,L]
RewriteCond %{THE_REQUEST} ^GET\s.+\.php [NC]
RewriteRule ^(.+)\.php$ /$1 [NE,R=301,L,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
Also remember that these rules will have NO impact in your php includes eg: include 'myheader.php';
It is because those includes are processed by php itself and don't go through Apache.
The following RewriteRules will assume the PHP extension for any file that is not otherwise matched. It should go at the bottom of your RewriteRules in your .htaccess file.
# Force PHP extension if not a directory
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^(.*)$ - [L]
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^((.*/)*[^./]+)/*$ $1.php [L]
This is for URL Rewriting. As far as include statements, you still need the PHP extension as these are physical file paths. If you need to so something fancy there, you should look into symbolic links.
You are not taking the correct path for fixing the problem you trying to solve.
If I understood well, what you want to do is having an url like :
http://example.com/my/url
instead of (for example):
http://example.com/my/url.php
If this is the case, what you should look for is rewrite url