Apache, rewrite and .htaccess misterium - apache

I've following .htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^plugins/.* pluginLoader.php [L]
RewriteRule ^settings\.php index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} \.art$ [OR]
RewriteCond %{REQUEST_FILENAME} /system/
RewriteRule .* index.php [L]
It works, how expected, until the URL points to a name of existing file without extension situated in plugins directory. I don't know why. There is for example a /plugins/invoice/name.txt file.
http://localhost/plugins/invoice/name.txt
uses pluginLoader.php as expected
http://localhost/plugins/invoice/name.
uses pluginLoader.php as expected
http://localhost/plugins/invoice/name
uses index.php! Why?
http://localhost/plugins/invoice/nam
uses pluginLoader.php as expected
The same applies for all files having .txt or .php extension. If the file has .sql extension, it neither uses pluginLoader.php nor index.php. It sends 404 - not found.
Is there some pre-processor?
What's is also interesting:
RewriteEngine On
RewriteBase /
RewriteRule ^plugins/.* pluginLoader.php [L]
RewriteRule ^settings\.php index.php [L]
To delete last four lines makes it working. But URL http://localhost/plugins/invoice/fill still gets a 404 error. When the file has been renamed, the URL works.
Mystery...

Replace your .htaccess code with this and see if that helps:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^plugins/ pluginLoader.php [L,NC]
RewriteRule ^settings\.php/?$ index.php [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} \.art$ [OR]
RewriteCond %{REQUEST_FILENAME} /system/
RewriteRule ^ index.php [L]
Make sure there is no .htaccess in $DOCUMENT_ROOT/plugins directory.

Related

Hide folder in path using .htaccess file

I'm looking to hide the folder called contents in the URL path http://example.com/contents/folder-1/
My folder structure looks like:
/
--contents/
----folder-1/
------index.php
----folder-2/
------index.php
----folder-3/
------index.php
index.php
I have full root access to the server, enabled a2enmod rewrite, restarted apache. I then put the following .htaccess file in the / directory.
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+contents/([^\s]+) [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?!^contents/)^(.*)$ /contents/$1 [L,NC]
However, when I go to http://example.com/folder-1/ it just gives me a 404 page error.

Apache rewrite /product/ to /products/ and index.php

I've been asked to help on an ExpressionEngine site that's been developed. There is an existing .htaccess file that rewrites anything that isn't a file or directory so that it goes via index.php:
RewriteEngine On
RewriteBase /
# Not a file or directory (also catches index.php)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?$1 [L]
This works fine, and an example URL might be:
www.mysite.com/products/foo (plural 'products')
However, It also needs to deal with old URLs which will be in the form:
www.mysite.com/product/foo (singular 'product')
I've tried the following:
RewriteEngine On
RewriteBase /
# Not a file or directory (also catches index.php)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^product/(.+) /index.php/products/$1 [L]
RewriteRule ^(.*)$ /index.php?$1 [L]
Hoping that if it finds 'product' then it rewrites to 'products' via index.php and then quits, but it's not working.
At the same time, I need any incoming URL that starts:
/department/foo
to be rewritten to:
/departments/category/foo
Any help greatefully received.
You can use add a rule above your existing rule to for mapping product -> products:
RewriteEngine On
RewriteBase /
RewriteRule ^product(/.*)?$ products$1 [L,NC]
# Not a file or directory (also catches index.php)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?$1 [L]

Htaccess causes folder loops

Htaccess rules on my website:
DirectoryIndex /index.php
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{HTTP_HOST} ^(www\.)*(([^\.]*)\.)tst\.url [NC]
RewriteRule ^(.*)$ http://tst.url/%3/$1 [L]
ErrorDocument 403 /
It's creating deeper cascading loop (from: /foo/bar.php - towards: /foo/foo/.../foo/bar.php when clicking any link).
Links are opening index.php file - instead of bar.php files.
Websites like:
http://htaccess.madewithlove.be/
http://martinmelin.se/rewrite-rule-tester/
don't show any problems.
What could be wrong?
Is there any non site-affecting way to test htaccess file custom modifications?

Trying to apply mod rewrite rule for e.g. css/style.123456.css -> css/style.min.css

I'm trying to create a rewrite rule for any js/css files with a given time stamp to point to the real files but not really sure how to do it. Below is my attempt but when I try this it seems to mess up my
AddDefaultCharset UTF-8
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(js|css)\/(.+)\.(\d+)\.(js|css)$ $1/$2.min.$4
RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
</IfModule>
By the way, my folder structure is as follows:
/public/
/public/index.php
/public/.htaccess (rules are written in this file)
/public/css/style.min.css
/public/js/all.min.js
Also, the apache vhost conf file will direct requests to the /public/ directory. This I can verify works. But when I try to view /css/style.12235543543.css (which I want to point to /css/style.min.css) it just goes through the front controller (index.php) instead of the stylesheet.
Use that:
AddDefaultCharset UTF-8
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^(js|css)/(.+)\.(\d+)\.(js|css)$ $1/$2.min.$4 [L]
RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
</IfModule>

strange redirect with url rewrite

My .htaccess file is:
RewriteEngine on
RewriteRule (.*) index.php?_url=$1 [QSA,L]
It works perfectly in most cases. But if the requested url exists as a real folder, it goes crazy.
For example when i request /random it is rewritten to index.php?_url=random with status code 200, but for /static (which is an existing directory on the web server) it gives a 301 redirect to /static/?_url=static
use this below code for .htaccess
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
If you want to exclude existing files and directories from being rewritten, do so with a RewriteCond
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?_url=$1 [QSA,L]