In PHP MVC, How to show external css, js and image files? - php

In PHP MVC, How to show external css, js and image files?
RewriteEngine On
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteCond %{REQUEST_URI} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
I could not show any external files.

The code above matches the ${REQUEST_URI}, e.g. matches at all uris, where you probably want to match the requested filename ${REQUEST_FILENAME}
Moreover, the code above also matches non existent symlinks ("!-l") and non existent directories ("!-d").
Probably only matching non-existent filenames would work perfectly:
RewriteEngine On
RewriteCond ${REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1
Now, only requested filenames that don't physically exist on in the folder index.php resides in will be aliased to index.php.
For speed purposes, or if Apache rejects to display your static css and such you can disable the RewriteEngine on a given directory, for example:
<Directory "path/to/public_html">
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1
</Directory>
<Directory "path/to/public_html/static">
RewriteEngine Off
</Directory>
The order of the directories might be the other way around if the example above doesn't work. Also make sure to add this to your .htaccess file or in your <VirtualHost>-tag.

Related

hide (remove) folder names in URL rewrite Rules Not working

I want to make clean URLs removing folder names with htaccess and I have multiple folders (food, health,beauty .....) rewrite rules not working
site.com/food/one.php
TO
site.com/one.php
Rule for food folder
RewriteEngine On
<IfModule mod_rewrite.c>
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /food/$1 [NC,L]
</IfModule>
Rule for health folder
<IfModule mod_rewrite.c>
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /health/$1 [NC,L]
</IfModule>
Rule for beauty folder
<IfModule mod_rewrite.c>
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /buety/$1 [NC,L]
</IfModule>
only one Rule works fine ( top rule only... whatever it may be ) rest of the rules not
working, getting internal server error 500
Please Help me out
Assuming all your rule blocks are one after the other in the root .htaccess file.
rest of the rules not working, getting internal server error 500
Because you are unconditionally rewriting to the /food/ subdirectory (because this rule is first) regardless of whether the target file exists or not. If it doesn't exist it will create a rewrite loop, which results in a 500 Internal Server Error response.
The remaining rules are only processed if the request already maps to a file (or directory), but then the preceding RewriteCond directives prevent the rule from doing anything (since it has already been established that the request maps to a file or directory).
If there is no discernable difference in the URL then you need to check that the target file exists before rewriting the request.
You can do this, but with the obvious caveat that you can't have the same filename in more than one subdirectory (the first will always "win"). We also need to make some assumptions...
You are rewriting requests for physical .php files only (as per your example).
.php is present in the URL (as per your example).
Try the following instead:
RewriteEngine On
# If the request is already for one of the subdirectories then stop here
RewriteRule ^(food|health|beauty)($|/) - [L]
# If the request is for anything other than a ".php" file then stop here
RewriteRule !\.php$ - [L]
# If the request already maps to a file (or directory) then stop here
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# Rewrite to "/food" if the target file exists
RewriteCond %{DOCUMENT_ROOT}/food/$0 -f
RewriteRule .+ food/$0 [L]
# Rewrite to "/health" if the target file exists
RewriteCond %{DOCUMENT_ROOT}/health/$0 -f
RewriteRule .+ health/$0 [L]
# Rewrite to "/beauty" if the target file exists
RewriteCond %{DOCUMENT_ROOT}/beauty/$0 -f
RewriteRule .+ beauty/$0 [L]
There is no need for the <IfModule> wrappers or the RewriteBase directive(s).
Although it does seem a bit odd that you would want to remove these subdirectories from the URL, as they look like "keywords" that would be better left in the URL?

Rewrite all request to index.php (Including existing file and directory paths)?

I am working on a PHP application that I'd like to regulate file serving for various reasons. I need all requests to be passed to my index.php file. Currently I have this working for all requests except files and directories that exist. For security reasons and content protection I'd like to perform some challenges before serving files to the browser using readfile and I'd like to completely block directory access which will just show a 404 page for files and directories I don't want users to see (i.e., private content, php scripts, etc.).
This is my current HTACCESS file:
# Turn On Rewrite Engine
RewriteEngine On
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_URI} !^.*$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
I don't no what to do with the RewriteCond that is causing files to be mapped to the index.php file if they don't exists. I'd like them to remap there whether or not they do exist.
For example, navigating to "http://www.example.com/some/real/script.php" would redirect the path to PHP regardless if it exists or not. Thanks guys in advance.
This one worked for me
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteCond %{REQUEST_URI} !/folder
RewriteRule ^(.*)$ index.php?request=$1 [L,QSA]
</IfModule>

.htaccess PHP - Prevent rewriting when only domain is entered

I have a site located at
application
controllers
views
...
public <- the root of the site
index.php <- entry point
css
...
So the url is http://localhost/application/public. I would like when the user enters only http://localhost/application/public to do nothing except calling index.php without showing it.
Here are some examples:
http://localhost/application/public -> http://localhost/application/public or http://localhost/application/public/
BUT
http://localhost/application/public/asdf -> Rewrite internally to http://localhost/application/public/index.php and show only http://localhost/application/public/asdf
I have written the .htaccess. The only problem is that when entering http://localhost/application/public it opens http://localhost/application/public/index.php instead of http://localhost/application/public
One more thing - I want existing directories to be rewritten, so only existing files should not be rewritten to index.php So basically http://localhost/application/public/css should be rewritten to index.php. The reason I want this is to ommit the 403 Forbidden error, and kindly tell the user that the path does not exist.
HTACCESS
Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine On
RewriteBase /application/public/
#Remove trailing slash
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ $1 [R=301,L]
#Do the rewriting
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [NC,L]
The reason I put RewriteCond %{REQUEST_FILENAME} !-d is to prevent http://localhost/application/public from rewriting. If http://localhost/application/public is rewritten, it loads http://localhost/application/public/index.php
Actually I know there are needed some modifications on RewriteCond %{REQUEST_FILENAME} !-d. I want the root, public folder to not get rewritten, it should be the only exception. All of the rest physical folders like css, js, have to be rewritten, like not existing. Files should not be rewritten. Thanks!
Ok, I think I found a solution.
Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine On
RewriteBase /application/public/
RewriteRule ^(css/?|js/?)$ index.php [NC,L] <- Folders css, js will be rewritten, but root directory won't be rewritten. Files inside them also won't be rewritten.
#Remove trailing slash
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ $1 [R=301,L]
#Do the rewriting
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [NC,L]

Rewrite URLs without file extension (including across sub-directories)

I have the following .htaccess file on my server (It's a tweak of code I found on-line somewhere, for the life of me I cannot remember where) and I'm not quite sure how to amend it:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
It basically grabs the URL, removes any trailing /, adds .php to the end of it, goes to that page, but then displays the link with a / instead of the extension in the URL bar (from what I can tell).
It works really well, bar two annoying niggles:
I can then only have .php files within that directory. Any others break.
The .htaccess file needs to be copied to EVERY subdirectory to function.
Can this script be improved upon at all, to do the following:
Put it in my root (/) directory and have it work in to all sub-folders?
Allow multiple file extensions within each directory? (e.g. some .php, some .html)
This code should work from your DocumentRoot and will add all the extensions:
Options +MultiViews
RewriteEngine On
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule ^(.+)$ /$1/ [R=301,L]

Deny access to all files except one from subdirectory

I am trying to restrict access to all files in my home directory, then to allow only one main php file from a subdirectory using one .htaccess file, but it does not work as expected. It shows only "Forbidden" for every request. How to make it work as expected?
Now I am using :
order allow,deny
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/$ [OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* /path_to_file/index.php [QSA]
You can just forward all requests to that particular file. Put this in your root .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_URI} !/subdir/file\.php$ [NC]
RewriteRule ^ /subdir/file.php [QSA]

Categories