Htaccess causes folder loops - apache

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?

Related

How to redirect URL into a new URL using .htaccess?

I want to redirect pages using .htaccess. I am using code igntiter
When user type this URL in addresss bar :
form.bixy.com/salesapp
and they hit enter button, the URL should changed into:
form.bixy.com/salesapp/administrator/addusr
I have this .htaccess inside the salesapp folder
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond $1 !^(index\.php|images|stylesheets|system/application/sources/)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/
</IfModule>
If the line RewriteRule ^(.*)$ index.php?/ is removed, I got The requested URL was not found on this server.
I have little knowledge about .htaccess. Any help is greatly appreciated, thanks in advance.
Edit:
This is my current .htaccess that I put inside salesapp folder:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond $1 !^(index\.php|images|stylesheets|system/application/sources/)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php [L]
RewriteRule ^/? /salesapp/administrator/addusr[NC,R=301,L]
</IfModule>
It still give me error, but the URL should've worked
The page isn’t redirecting properly
Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
Inside salesapp you can try this .htaccess:
RewriteEngine on
RewriteBase /salesapp/
RewriteRule ^(?:administrator)?/?$ administrator/addusr [R=301,L,NC]
RewriteCond $1 !^(index\.php|images|stylesheets|system/application/sources/) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php [L]

htacess RewriteRule http://domain/folder/(any searchstring) to redirect to http://domain/folder/index.php?option=$1

I am trying to create a rewrite rule in my Apache .htaccess file and the goal is:
http://domain/folder/(any search string) to redirect to http://domain/folder/index.php?option=$1
example:
http://domain/folder/covid to redirect to http://domain/folder/index.php?option=covid
I got this setup in htacess in http://domain/folder, but somehow it's not working:
RewriteEngine On
RewriteRule ^([^/folder]+)/([^/]+)$ http://domain/folder/index.php?option=$1 [L]
The website is hosted on http://domain/folder
Can someone help me on this?
You may try this .htaccess inside folder/:
RewriteEngine On
RewriteRule ^index\.html?$ index.php [L,NC]
RewriteRule ^index\.php$ - [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .+ index.php?option=$0 [L,QSA]
You could have your htaccess file like as follows. Please keep your htaccess file along side with your folder(named folder) and NOT inside folder.
Please make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php?option=$1 [L]
OR in case your htaccess is present inside folder folder then try following.
RewriteEngine ON
RewriteBase /folder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php?option=$1 [L]

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]

mod_rewrite trouble - RewriteEngine On gives 403 when trying to access subdirectory

I've got a webshop(xtCommerce) running on a shared hosting webspace.
The root directory (where shop 1 is installed, up and running) contains the .htaccess file provided by the shop software
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/media/
RewriteCond %{REQUEST_URI} !^/extAdmin/
RewriteCond %{REQUEST_URI} !^/skin/
RewriteCond %{REQUEST_URI} !^/js/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php
I now installed a second webshop (also xtcommerce), within the same webspace, at /shop2 which contains the same .htaccess file
www.shop1.net -> /
www.shop2.net -> /shop2
-Trying to access shop2 results in 403
-When I'm removing RewriteEngine on from the .htaccess in /shop2 the frontpage displays just fine, but the links seem to be broken.
I don't know much about mod_rewrite, I tried adding RewriteBase /shop2 to the 2nd .htaccess file, but it failed.
Thank you in advance!
EDIT: Even when I use RewriteRule .* http://google.de in the root directory, and remove the .htaccess in /shop2 I keep getting "Forbidden". Maybe the problem isn't the rewriting?
EDIT2: Current status:
.htaccess in /
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/media/
RewriteCond %{REQUEST_URI} !^/extAdmin/
RewriteCond %{REQUEST_URI} !^/skin/
RewriteCond %{REQUEST_URI} !^/js/
RewriteCond %{HTTP_HOST} !shop2\.net$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php
.htaccess in /shop2 (4 different examples)
RewriteRule .* index.php # frontpage shop2.net works, any URLs to be rewritten result in 404
RewriteRule .* shop2/index.php # frontpage shop2.net works, any URLs to be rewritten result in 404
RewriteRule .* http://google.de # frontpage shop2.net works, any URLs to be rewritten result in 404
#RewriteRule .* index.php # shop2.net => forbidden
Rather strange setup of virtual host you have there. The problem is the htaccess in the root is executed before the one in /shop2. The root htaccess rewrites the the index.php file in the root. But because the document_root for shop2.net is /shop2, it is the index.php file is outside of the document_root, so apache forbids this (as it should).
To fix this put this in the root htaccess.
RewriteEngine on
RewriteCond %{HTTP_HOST} !shop2.net$
RewriteCond %{REQUEST_URI} !^/media/
RewriteCond %{REQUEST_URI} !^/extAdmin/
RewriteCond %{REQUEST_URI} !^/skin/
RewriteCond %{REQUEST_URI} !^/js/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php
But better just create a separate folder for shop2, instead of a sub-folder of shop1

Apache, rewrite and .htaccess misterium

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.