.htaccess file's puzzling action (code inside) - apache

I have this CMS set up in a folder called localhost:5999/madison/
The .htaccess file (code below) is in the same folder localhost:5999/madison/
If, in a browser, I go to localhost:5999/madison/ or to localhost:5999/madison/index.php or to localhost:5999/madison/xyz where xyz is anything not containing forward slashes, it redirects me to the following file: localhost:5999/madison/inc/views/view-404.php
My question is: Why?
.htaccess code:
RewriteEngine On
ExpiresActive On
#Expire Header
<FilesMatch "\.(ico|jpg|jpeg|png|gif|js|css|swf)$">
ExpiresDefault "now plus 7 days"
</FilesMatch>
ExpiresByType text/css "now plus 7 days"
ExpiresByType text/html "now plus 7 days"
ExpiresDefault "now plus 7 days"
AddOutputFilterByType DEFLATE text/plain text/html text/xml text/css
RewriteRule ^(assets|inc) - [L]
RewriteRule ^admin[/]?$ admin/index
RewriteRule ^admin/edit/(.*)$ index.php?type=admin&action=edit&page=$1 [L]
RewriteCond %{QUERY_STRING} ^(.*)?$
RewriteRule ^admin/(.*)$ index.php?type=admin&action=view&page=$1 [L]
RewriteCond %{QUERY_STRING} ^(.*)?$
RewriteRule ^login$ index.php?action=view&type=login&%1 [L]
RewriteCond %{QUERY_STRING} ^(.*)?$
RewriteRule ^forgot-password$ index.php?action=view&type=forgot-password&%1 [L]
RewriteCond %{QUERY_STRING} ^(.*)?$
RewriteRule ^edit/user(/)?$ index.php?action=edit&type=user&%1 [L]
RewriteCond %{QUERY_STRING} ^(.*)?$
RewriteRule ^signup(/)?$ index.php?action=edit&type=user&id=0&%1 [L]
RewriteCond %{QUERY_STRING} ^(.*)?$
RewriteRule ^user/([0-9]+)(/)? index.php?action=view&type=note&user=$1&%1 [L]
RewriteCond %{QUERY_STRING} ^(.*)?$
RewriteRule ^(suggestion|comment)/([0-9]+)(/)? index.php?action=view&type=note&page=open&note_type=$1&note=$2&%1 [L]
RewriteCond %{QUERY_STRING} ^(.*)?$
RewriteRule ^([0-9A-Za-z\-_]+)/(suggestion|comment)/([0-9]+)(/)? index.php?action=view&type=note&page=$1&note_type=$2&note=$3&%1 [L]
#Catch All Pages
RewriteRule ^index\.php$ - [L]
RewriteCond %{QUERY_STRING} ^(.*)?$
RewriteRule ^([0-9A-Za-z\-_]+)(/)? index.php?action=view&type=page&page=$1&%1 [L]

The issue arises because you are hosting the CMS in a sub-folder localhost:5999/madison/ (without changes to the configuration file) and not directly in localhost:5999/
To solve the issue, make the following changes to config.php in /madison/inc/ directory:
config.php
/**
* Server Definitions
*/
define('SERVER_ABS', $_SERVER['DOCUMENT_ROOT']);
define('SERVER_URL', 'http://'.$_SERVER['HTTP_HOST']);
To:
/**
* Server Definitions
*/
define('SERVER_ABS', $_SERVER['DOCUMENT_ROOT'].'/madison');
define('SERVER_URL', 'http://'.$_SERVER['HTTP_HOST'].'/madison');

That seems to be borne out for some custom 404 handler outside your current .htaccess.
Try adding these lines on top of your .htaccess:
DirectoryIndex index.php
RewriteEngine On
RewriteBase /madison/
And keep your last rule as:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ index.php?action=view&type=page&page=$1 [L,QSA]
Then test this in a new browser to avoid old caches.

Related

After htaccess changes the css stop working as stylesheet

I have a weird issue with my css files.
After i changed the htaccess file to this
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{THE_REQUEST} /eventDetails\.php\?id=([0-9]+)&name=(.+)\sHTTP [NC]
RewriteRule ^ /%1/%2? [L,R]
RewriteRule ^([^/]+)/([^/]+)/?$ /eventDetails.php?id=$1&name=$2 [L]
</IfModule>
my css files stoped loading to ALL the pages of the website.
My public_html folder contains
index.php
profile.php
css -> style.css
js -> owl.carousel.js
.htaccess
With Google Chrome Developer Tools i have this error for the css files
Resource interpreted as Stylesheet but transferred with MIME type text/html: "http://something.gr/css/style.css".
and this error for the javascript files
owl.carousel.js:1 Uncaught SyntaxError: Unexpected token <
Some images from the Network Tab.
WITHOUT .htaccess file
As you can see the Content-Type is text/css .. like it should be
WITH .htaccess file
As you can see here the mime.type returns it as a text/html
What i've tried.
First. after <title> html tag i've enternet
<base href="/">
Second. I've tried to change the .htaccess file to this
<IfModule mod_rewrite.c>
RewriteEngine on
AddType application/javascript js
AddType text/css css
RewriteCond %{THE_REQUEST} /eventDetails\.php\?id=([0-9]+)&name=(.+)\sHTTP [NC]
RewriteRule ^ /%1/%2? [L,R]
RewriteRule ^([^/]+)/([^/]+)/?$ /eventDetails.php?id=$1&name=$2 [L]
</IfModule>
And lastly i've checked on my dedicated server usr/local/apache/conf/
on the mime.types file that the text/css css is NOT commented the same as
application/javascript js
I dont know what else i should do.
Your second rule
RewriteRule ^([^/]+)/([^/]+)/?$ /eventDetails.php?id=$1&name=$2 [L]
rewrites /css/style.css to /eventDetails.php?id=css&name=style.css.
If you want to exclude existing files from this rule, you may prefix it with
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
You may also change the first rule to
RewriteCond %{QUERY_STRING} id=([0-9]+)&name=(.+)
RewriteRule ^eventDetails\.php$ /%1/%2? [L,R]
and to prevent a redirect loop we insert at the beginning
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
You don't need this of course, if you keep the existing condition and rule against THE_REQUEST.
So the complete rules become
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteCond %{QUERY_STRING} id=([0-9]+)&name=(.+)
RewriteRule ^eventDetails\.php$ /%1/%2? [L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ /eventDetails.php?id=$1&name=$2 [L]

Trailing Slash 301 Redirect Issue When ? Is In URL

htaccess | 301 redirect | URI Issue
I recently applied this code to my htaccess to enforce the trailing slash on my URL:
#Trailing backslash
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L,R=301]
The code works great at adding a / at the end of MOST URLs except for those with ? in the middle of the URL. Like below:
https://www.example.com/list-of-all-objects/search?keywords=test
This URL with the currently code that is stated above will change on load to this: https://www.example.com/list-of-all-objects/search/?keywords=test
When it should look like this: https://www.example.com/list-of-all-objects/search?keywords=test/
I have quickly learnt all of the code that I have applied does not work for two reasons.
They don't enforce the / - they just make it possible if its blocked
The term REQUEST_URI or $1 or $2 all don't include ?keyword= (in fact there is nothing that I can find that refers to this part of the URL so I can't include it correctly)
No matter what I do - I can't get a ? or anything after it to be placed before the /. HOW do I get the trailing slash to not be applied before the ? on keyword search but at the end of the URL and continue to be applied at the end of all the other URL's?
Other previously tried methods:
# Force Trailing Slash
enter code hereRewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]
Found here: .htaccess Rewrite to Force Trailing Slash at the end
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ /$1/ [L,R=301]
Found here: Htaccess: add/remove trailing slash from URL
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
</IfModule>
Found here: http://www.paulund.co.uk/using-htaccess-to-force-trailing-slash
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [L,R=301]
Found here: http://ralphvanderpauw.com/seo/how-to-301-redirect-a-trailing-slash-in-htaccess/
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !example.php
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://domain.com/$1/ [L,R=301]
Found here: http://enarion.net/web/htaccess/trailing-slash/
# Trailing slash check
# Don't fix direct file links
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L,R=301]
Found here: https://moz.com/community/q/trailing-slash-at-end-of-url
# Always append a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [R=301,L]
Found here: https://craftcms.stackexchange.com/questions/9501/force-trailing-slash-on-urls
Below is my complete htaccess file contents:
RewriteEngine On
#Trailing backslash
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L,R=301]
#HTTPS Redirects
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* [QSA,L]
<IfModule mod_expires.c>
# Enable expirations
ExpiresActive On
# Default directive
ExpiresDefault "access plus 1 month"
# My favicon
ExpiresByType image/x-icon "access plus 1 year"
# Images
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
# CSS
ExpiresByType text/css "access plus 1 month"
# Javascript
ExpiresByType application/javascript "access plus 1 year"
</IfModule>
Query_string is not part of match in RewriteRule.
To add a trailing slash to query strings, you can use :
RewriteCond %{QUERY_STRING} ([^/]+)$ [NC]
RewriteRule ^ %{REQUEST_URI}?%1/ [R,L]
EDIT :
As I already said, query strings can not be handled directly by a RewriteRule directive, so you need a separate rule to add trailing slash to query strings.
The example bellow adds a trailing slash to both parts of the url.
RewriteEngine on
#Add a trailing slash to uri
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^ %{REQUEST_URI}/ [NC,L,R]
#Add a trailing slash to query strings
RewriteCond %{QUERY_STRING} ([^/]+)$ [NC]
RewriteRule ^ %{REQUEST_URI}?%1/ [R,L]
This will change the following urls :
http://example.com/file
to
http://example.com/file/
Or
http://example.com/file?q=foo
to
http://example.com/file/?q=foo/
(Hope, this helps!)

How to redirect .htaccess to two MVC trees

What I have tried to achieve with apache mod rewrite is the following:
When I access localhost/system/this/is/a/test/, the .htaccess mod rewrite should redirect to my index.php
When I access localhost/system/api/this/is/a/test/, the .htaccess mod rewrite should redirect to my api.php
I have been trying this for several hours, but i can not seem to get it working the way that I would like. Currently all access is still pointing to index, and no matter what I try I can not seem to get the .htaccess to point to both the api and the index depending on the URL.
The following is my current .htaccess file:
ExpiresActive On
ExpiresByType image/png "access plus 2 weeks"
ExpiresByType image/jpg "access plus 2 weeks"
ExpiresByType image/jpeg "access plus 2 weeks"
AddType application/x-httpd-php .xml
Options +FollowSymLinks
Options -Indexes
RewriteEngine On
RewriteBase /system/
# file access exclusions
RewriteRule ^(?!sitemap\.xml$).(.*)\.xml$ [R=404,L]
RewriteRule ^(.*)\.log$ [R=404,L]
RewriteRule ^(.*)\.txt$ [R=404,L]
# Rewrite the request to API
RewriteCond ${REQUEST_URI} ^api/(.*)$ [NC]
RewriteRule ^(.*)$ api.php/$1 [L]
# Rewrite the request to index
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
Can any of you explain to me what I am doing wrong? That would be a great help and greatly appreciated.
Thank you in advance
Replace your rules with this:
RewriteEngine On
RewriteBase /system/
# file access exclusions
RewriteRule ^(?!sitemap\.xml$).(.*)\.xml$ [R=404,L]
RewriteRule ^(.*)\.(txt|log)$ - [R=404,L]
# Rewrite the request to API
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^api/(.*)$ api.php/$1 [L]
# Rewrite the request to index
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]

.htaccess DirectoryIndex not working

I have a website running on Linux/UNIX but I don’t know a darn thing about the .htaccess file and the syntax used in it..
If you go to http://pr.bananapages.net the system won’t produce any results.
But if you add the file name, it comes up. ex: http://pr.bananapages.net/index.html
NOTE: The URL points to a subfolder on the shared access hosting server but it doen't really matter because when you type in www.bananapages.net/pr/hamshack, (That's where the short cut DNS address points to), It still won't produce the index.html file without actually specifying it.
I have tried everything I could find about DirectoryIndex but I can’t get it to work without using the file name… (index.html)
This is my current .htccess file.
Options -MultiViews
DirectoryIndex system.php index.php
<IfModule mod_rewrite.c>
DirectoryIndex system.php index.php index.html
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)\.(.*)$
RewriteRule ^(.+[^/])$ http://%{HTTP_HOST}%{REQUEST_URI}/ [L,R=301]
# Google sitemap controller
RewriteRule ^sitemap.xml$ tmp/sitemap.xml [L]
RewriteRule ^tmp/sitemap.xml$ tmp/sitemap.xml [L]
RewriteRule ^index.html$ index.html [L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*)(\.xml|\.php([0-9]*)|\.tpl|\.phtml|\.ini|\.inc|/)$ system.php?_p=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ system.php?_p=$1 [QSA,L]
</IfModule>
# compresses text, html, javascript, css and xml
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript
AddOutputFilterByType DEFLATE application/xml application/xhtml+xml application/rss+xml
AddOutputFilterByType DEFLATE application/javascript application/x-javascript
</IfModule>
NOTE: If I rename the .htaccess file, it will work without specifying the filename, (index.html) so. I am fairly certain there is something in this .htaccess file preventing this from working the desired way.
You can try this:
in all your
RewriteCond %{REQUEST_FILENAME} !-f
Double it as thus, to say to do rewrite only if lt's neither directory, nor file:
Edit
(Tested in Debian/Apache2).
Options -MultiViews
DirectoryIndex system.php index.php index.html
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)\.(.*)$
RewriteRule ^(.+[^/])$ http://%{HTTP_HOST}%{REQUEST_URI}/ [L,R=301]
# Google sitemap controller
RewriteRule ^sitemap.xml$ tmp/sitemap.xml [L]
#RewriteRule ^tmp/sitemap.xml$ tmp/sitemap.xml [L]
#RewriteRule ^index.html$ index.html [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)(\.xml|\.php([0-9]*)|\.tpl|\.phtml|\.ini|\.inc|/)$ system.php?_p=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ system.php?_p=$1 [QSA,L]
to see if it works better? Please notice some commented infinite loop rewriterule.
I had a similar situation on my vps
I wanted my default homepage to be login.php so if someone where to go to http://[ip]/[folder]/ it would just load the login.php file that I set in my .htaccess within the [folder]
so i set 'DirectoryIndex login.php' in .htaccess but
it would not work instead I would get a 403 when going to http://[ip]/[folder]/ as I didn't have any index.html or index.php
so...
I decided to check the apache config file httpd.conf and under DirectoryIndex I put 'login.php' as part of the list that was already there then restarted the httpd service and
it worked for me!
Here's the answer...
# Rules for subfolders
# for pr.bananapages.net
RewriteRule ^pr/(.*)$ pr/$1 [L]
# for jm.bananapages.net
RewriteRule ^jm/(.*)$ jm/$1 [L]
# and so on....
RewriteRule ^dr/(.*)$ dr/$1 [L]
RewriteRule ^usvi/(.*)$ usvi/$1 [L]
RewriteRule ^bvi/(.*)$ bvi/$1 [L]
RewriteRule ^kitts/(.*)$ kitts/$1 [L]
RewriteRule ^ar/(.*)$ ar/$1 [L]
RewriteRule ^tt/(.*)$ tt/$1 [L]
I don't know why that works but it does seem to work...
Dale

Remove trailing slash after domain

This is my .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Remove multiple slashes anywhere in URL
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
# Never use www prefix!
RewriteCond %{HTTP_HOST} ^www.domain\.org [NC]
RewriteRule (.*) http://domain.org/$1 [R=301,L]
# Remove multiple slashes after domain
RewriteRule ^/(.*)$ http://domain.org/$1 [R=301,L]
# Remove trailing slash in some cases
RewriteRule ^(.*)\.css/$ http://domain.org/$1.css [L,R=301]
RewriteRule ^(.*)\.js/$ http://domain.org/$1.js [L,R=301]
RewriteRule ^(.*)\.jpg/$ http://domain.org/$1.jpg [L,R=301]
RewriteRule ^(.*)\.jpeg/$ http://domain.org/$1.jpeg [L,R=301]
RewriteRule ^(.*)\.png/$ http://domain.org/$1.png [L,R=301]
RewriteRule ^(.*)\.gif/$ http://domain.org/$1.gif [L,R=301]
RewriteRule ^(.*)\.xml/$ http://domain.org/$1.xml [L,R=301]
# Force trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !index.php
RewriteCond %{REQUEST_URI} !(.*)\.css
RewriteCond %{REQUEST_URI} !(.*)\.js
RewriteCond %{REQUEST_URI} !(.*)\.jpg
RewriteCond %{REQUEST_URI} !(.*)\.jpeg
RewriteCond %{REQUEST_URI} !(.*)\.png
RewriteCond %{REQUEST_URI} !(.*)\.gif
RewriteCond %{REQUEST_URI} !(.*)\.xml
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://mydomain.org/$1/ [L,R=301]
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
# MIME types
AddType text/css .css
AddType text/javascript .js
# Enable compression
AddOutputFilterByType DEFLATE text/html text/plain text/css text/javascript text/x-css text/x-javascript text/x-js text/htm application/x-javascript application/javascript application/js application/x-js image/png image/gif image/jpg image/jpeg
#Skip browsers with known problems
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
php_flag display_errors on
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
But, when I go to **/////, the trailing slashes will not go away. What am I doing wrong?
The %{REQUEST_URI} variable gets reduced of extra slashes when it gets prepped. So the condition RewriteCond %{REQUEST_URI} ^(.*)//(.*)$ will never match because for a request like http://domain.org////, the REQUEST_URI variable gets reduced to just /. Try using the THE_REQUEST variable:
RewriteCond %{THE_REQUEST} ^([A-Z]{3,9})\ (.*)//([^\ ]*)
RewriteRule ^ %2/%3 [R=301,L]
Additionally, the prefix (the leading slash) gets stripped off of the request URI when rewrite rules are in an htaccess file, so the rule RewriteRule . %1/%2 [R=301,L] would never match because the regex . requires at least one character to match. When the URI is / and the leading slash gets stripped, the URI that's used to match in the url is a blank string. So using ^, or (.*), or something equivalent of "everything including nothing" regex needs to be used.