.htaccess file blocks css, js but how? - regex

I have a .htaccess file inside an existing project folder. The code for the .htaccess file is below.
# Use PHP54 Single php.ini as default
#Options +FollowSymLinks
#Options +Indexes
RewriteEngine On
RewriteBase /
#RewriteRule ^users/([A-Za-z0-9_]+)$ users/index.php [NC,L]
#RewriteRule ^([A-Za-z0-9_]+)$ users/$1 [NC,L]
#RewriteRule ^ - [L]
#RewriteCond %{DOCUMENT_ROOT}/users/$1 -f
#RewriteRule .* users/$1 [L]
#Options -Indexes
#RewriteEngine On
#RewriteRule ^[!/.]*([A-Za-z0-9]+)/?$ /index.php?id=$1 [NC,L]
#<FilesMatch "\.(?i:vcf)$">
# ForceType application/octet-stream
# Header set Content-Disposition attachment
#</FilesMatch>
# If requested resource exists as a file or directory, skip next two rules
RewriteCond %{DOCUMENT_ROOT}/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule (.*) - [S=2]
#
# Requested resource does not exist, do rewrite if it exists in /users
RewriteCond %{DOCUMENT_ROOT}/users/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/users/$1 -d
RewriteRule (.*) users/$1 [L]
#
# Else rewrite requests for non-existent resources to /index.php
RewriteRule (.*) users/index.php?q=$1 [L]
So what happens it, when I try to open the project in url at localhost
http://localhost/projectfolder
it opens the index file without any javascript and css. Also the links to other pages do not open and the error message shows as -"Object not found". So I can only view index.php with out css and js files.
But when I remove / rename the .htaccess file, all works fine. So there must be something in this file which blocks all css, js files. So I am not asking for all the rules of writing a .htaccess file, I just need to know which line of code is blocking the files and how can I edit them, so it will not block any file wile working in localhost.
Note:- This question I am asking only for my knowledge. As I have written, if I remove the file then its working fine. But still I want to know which line of code is blocking the access to files. Thanks.

I suspect this rule is not doing what you think it should be doing:
RewriteCond %{DOCUMENT_ROOT}/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule (.*) - [S=2]
Replace this with:
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
Also you might be using relative links for css/js/images which will also cause 404 after rewriting to /users/. You have 2 options to fix that:
use absolute path in your css, js, images files rather than a relative one. Which means you have to make sure path of these files start either with http:// or a slash /.
You can try adding this in your page's HTML header:
<base href="/" />

Related

.htaccess how to remove file extensions and index files

I'm trying to achieve a few things through .htaccess ,but keep running into issues. Before you tell me I need to research better and there's already a solution on this or a different forum, please know I've already done that. I always try and figure out things on my own before coming here, but this one is truly stumping me. Everything I've tried has only partially worked. Any help or education here would be truly appreciated.
My site has the following simple structure:
(root)
| index.html
| .htaccess
|
|___portal-folder
| index.php
| home.php
|
|_____admin-folder
| index.php
I'm looking to achieve the following:
When a user navigates to any base directory, for instance site.com or site.com/portal-folder/ they don't see the index file name index.html or index.php in their browser.
Same holds true if the user navigates to the full URL site.com/index.html or site.com/portal-folder/index.php I would like the user to see site.com or site.com/portal-folder/ respectively in their browser.
Strip the file extension off all files in the browser. So for instance navigating to site.com/portal-folder/home.php would show as site.com/portal-folder/home in the browser
The following code I'm using kind of works, but I'm getting strange behavior. For instance:
navigating to site.com/portal-folder/index doesn't remove the index file name and show up as site.com/portal-folder/index instead of site.com/portal-folder/ in the browser
navigating to site.com/portal-folder/ doesn't remove the index file name and shows up as site.com/portal-folder/index.php in the browser.
navigating to site.com/portal-folder/index.php takes the user back to the root site.com
navigating to site.com/portal-folder/home works correctly, but navigating to site.com/portal-folder/home.php doesn't strip the .php extension off.
navigating to site.com works correctly, but navigating to site.com/index.html doesn't remove the index file name.
RewriteEngine On
DirectoryIndex index.html index.php
# remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^(.+?)/$ /$1 [R=301,L]
# To internally forward /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]
Server Information: Apache Version 2.4.46
Have it this way:
DirectoryIndex index.html index.php
RewriteEngine On
# To externally redirect /dir/file.php to /dir/file and optionally remove index
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index|(\S+?))(?:\.php|\.html)?[/\s?] [NC]
RewriteRule ^ /%1%2 [R=301,L,NE]
# remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.+)/+$
RewriteRule ^ %1 [R=301,NE,L]
# To internally forward /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

what's wrong with the following htaccess rules

Hi I got an amazing (?) work to fix the error on a site. It was a HTTP 500 error.
It is caused by the htaccess file. But I can't figure out what's wrong with the code. here is the code.
# Use PHP5 Single php.ini as default
#AddHandler application/x-httpd-php5s .php
# The rules below basically say that if the file exists in the tree, just
# serve it; otherwise, go to index.php. This is more future-proof for your
# site, because if you start adding more and more content types, you don't
# need to alter the .htaccess to accomodate them.
# This is an important concept for the Front Controller Pattern which the
# ZF MVC makes use of.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ public/index.php [NC,L]
Redirect 301 /platforms/mt4-ecn http://example.com/
Redirect 301 /partnerships/mt4-to-mt4-bridge http://example.com/
<IfModule mod_suphp.c>
suPHP_ConfigPath /home/aaaecn/public_html
</IfModule>
<Files php.ini>
order allow,deny
deny from all
</Files>
this redirect rules are for zend framework, But I don't have any experience in working with that framework.
I commented all lines and tried now the 500 error is fixed. So I filtered it down by uncommenting.
RewriteRule ^.*$ - [NC,L]
this line is causing the error, but removing this giving invalid redirects and 404 errors. what can I do to fix the problem.
Try your rules like this:
RewriteEngine On
RewriteRule ^(partnerships/mt4-to-mt4-bridge|platforms/mt4-ecn)/?$ / [L,NC,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ public/index.php [L]

RewriteCond file exists

Such a simple problem but htaccess and I have never got along.
Serve the file if it exists.
If the URI is just / and if index.html exists then serve index.html
Otherwise serve app.php
Here is .htaccess:
# Disable the directory processing, Apache 2.4
DirectoryIndex disabled xxx
RewriteEngine On
# Serve existing files
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
# For / serve index.html if it exists
RewriteCond %{REQUEST_URI} ^/$
RewriteCond index.html -f
RewriteRule .? index.html [L]
# Otherwise use front controller
RewriteRule .? app.php [L]
It works if I comment out the RewriteCond index.html -f line as long as index.html does in fact exists. But I also want to check for index.php so I need the file exists check. And if nothing else I'd like to understand why the posted lines do not work.
This should work:
# Disable the directory processing, Apache 2.4
DirectoryIndex disabled xxx
RewriteEngine On
# Serve existing files
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# For / serve index.html if it exists
RewriteCond %{DOCUMENT_ROOT}/index.html -f [NC]
RewriteRule ^/?$ index.html [L]
# Otherwise use front controller
RewriteRule ^ app.php [L]
Remember that RewriteCond with -f or -d needs full path to the file or directory that's why you need to prefix the filename with %{DOCUMENT_ROOT}/. No need to escape dots here.

.htaccess remove .html file/directory priority rewrite

I'm trying to rewrite so you can access files without .html and without adding a trailing slash. here's the code being used:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.*?)/?$
RewriteCond %{DOCUMENT_ROOT}/%1.html -f
RewriteRule ^(.*?)/?$ $1.html
But for my file structure on the server where I have this:
/public_html
..
/beginners
beginners.html
and inside /beginners folder there's other files, e.g.
/beginners/page1.html
I need rewrites to work like this:
if user inputs url:
website.com/beginners - the server would return the contents of
file beginners.html currently it returns the contents of the directory /beginners . so i need the server to first check if beginners.html exists, if yes - then server serves beginners.html not the directory /beginners
if the user accesses url website.com/beginners/page1 the server should first check up if
page1.html exists in the folder beginners and if it finds the file page1.html then it serves the contents of the file page1.html
how can this be done?
Here's the .htaccess that does it:
Options +FollowSymLinks -MultiViews -Indexes
DirectorySlash Off
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*?)/?$
RewriteCond %{DOCUMENT_ROOT}/%1.html -f
RewriteRule ^(.*?)/?$ $1.html

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