Need help to fix my .htaccess file - apache

My index.php file is located at:
myserverHost/api/public/
which include api calls like getting token. like:
myserverHost/api/public/token
It works only if i type exact URL. i.e.
myserverHost/api/public/token
i am trying to write rewrite rule where i can use:
myserverHost/api/token
here what i have tried in my .htaccess file content:
RewriteEngine On
#RewriteBase /api/
RewriteRule .* - [env=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
# Make sure $HTTP_RAW_POST_DATA is deprecated warning does not appear
#php_value always_populate_raw_post_data -1

Try this, this will remove public in format public/x -> X
RewriteEngine On
#RewriteBase /api/
RewriteRule .* - [env=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ public/$1 [L]
RewriteRule ^ index.php [QSA,L]

Related

In .htaccess to prioritize DirectoryIndex to a RewriteRule

I want files to be accessed when they are available as specified in the DirectoryIndex line. E.g. when someone types in www.mywebsite.com/mydir/ it will redirect correctly. For this I have in my .htaccess the line:
DirectoryIndex index.html default.php
When a filename or directory does not exist I want it to redirect to a file (target_handler.php) which is located in the same directory as my .htaccess file. For this I have placed several lines after the DirectoryIndex:
RewriteEngine On
RewriteCond %{ENV:URI} ^$
RewriteRule ^(.*)$ - [ENV=URI:$1]
RewriteCond %{ENV:BASE} ^$
RewriteCond %{ENV:URI}::%{REQUEST_URI} ^(.*)::(.*?)\1$
RewriteRule ^ - [ENV=BASE:%2]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ %{ENV:BASE}target_handler.php [L,QSA]
What I want is for Apache to first check to see if there is an index.html or default.php available, and only upon unavailability perform a rewrite. Currently the behaviour seems random(?), so if someone types www.mywebsite.com/mydir/ then Apache sometimes does the rewrite first without checking if there is an index.html in the specified folder.
Help appreciated
PS: I am testing using: xampp
Apache/2.4.17 (Win32) OpenSSL/1.0.2d PHP/5.6.15
You can omit DirectoryIndex and do it all using mod_rewrite itself:
RewriteEngine On
RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$
RewriteRule ^(.*)$ - [E=BASE:%1]
RewriteCond %{DOCUMENT_ROOT}%{ENV:BASE}index\.html -f
RewriteRule ^ index.html [L]
RewriteCond %{DOCUMENT_ROOT}%{ENV:BASE}default\.php -f
RewriteRule ^ default.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^target_handler\.php$ target_handler.php [L,NC]

.htaccess: Serve static files, route everything else to index.php

I want to use an .htaccess file to check if the requested path is a file in the public/ directory. If yes, serve it, else forward request to /index.php. I can't seem to get this to work.
Here's what I've got:
Options +FollowSymLinks
RewriteEngine on
Options -Indexes
RewriteCond %{DOCUMENT_ROOT}/public%{REQUEST_URI} -f
RewriteRule ^ %{DOCUMENT_ROOT}/public%{REQUEST_URI} [L]
RewriteRule ^ index.php [QSA,L]
e.g. http://example.com/css/style.css should have apache serve /public/css/style.css because it's a file that exists, but http://example.com/css/style.bad should be sent to /index.php.
This code should be working as expected on your side
Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}/public/$1 -f
RewriteRule ^(.*)$ public/$1 [L]
RewriteCond %{THE_REQUEST} \s/public/ [NC,OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L,QSA]
Apparently [L] does not work as I expected. With that in mind, and a lot of trial and error, I managed to find something that works:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/public%{REQUEST_URI} -f
RewriteRule ^ public%{REQUEST_URI} [L]
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^ index.php [QSA,L]

mod_rewrite check public folder before passing to php

What I am trying to achieve (with mod_rewrite) is on request check if a file/directory exists in the public folder, if it does, serve it, if not, route the request to the index.php file. Also if no extension provided in the URL then default to .html.
Below is my folder structure:
/.htaccess
/index.php
/public
/test.html
/test.xml
/a_folder
/index.html
/test.html
So for example here are a few requests and responses:
example.com/test.xml >>> /public/test.xml
example.com/a_folder/ >>> /public/a_folder/index.html
example.com/a_folder/test >>> /public/a_folder/test.html
example.com/not_in_public >>> /index.php
Any pointers on this would be amazing, thanks in advance.
Something like this: (Heavily cribbed from my WordPress .htaccess file)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)^(\.\w+) $1.html [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
The $1.html line is off the top of my head and may need some tweaking. Note that this is a dumb check that the requested URL ends in a dot followed by one or more word characters.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^(|index\.php)$ - [L]
RewriteRule ^public/(.*)$ - [L]
RewriteRule ^(.*)$ public/$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [R]
</IfModule>
This should do the trick.
Basically, if it's / or /index.php it won't do anything, if it's /public/whateveryouwant it won't do anything, if it's anything else it will rewrite to /public/whateveryouwant and check if it's either a file or a directory. If it's not it will redirect to index.php.
Thanks to #jxpx777 and #N1xx1 after a bit of juggling made it work.
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteRule ^public/(.*)$ - [L]
# Check public cache
RewriteRule ^(.*)$ public/$1
RewriteRule (.*)/$ $1/index
# Add .html if not extension provided
RewriteCond %{REQUEST_URI} !\..*$
RewriteRule ^(.*)$ $1.html
# Push through stack in not in cache
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

getting error 500 with .htaccess when pointing a php file (mod_rewrite)

i made a simple .htaccess file to expose the problem.
I want that all request made to the folder redirect to a subfolder file "app/index.php"
RewriteEngine On
RewriteRule (.*) app/index.php [QSA]
And this works fine on with the basic url of my hoster (like http//myname.hosting.com/htaccessFolder
The problem is that i have a domain pointing on this folder, and when accessed to it, server return Internal Server Error 500.
If i do this :
RewriteEngine On
RewriteRule (.*) somefile.php [QSA]
Redirection works only when file does not exists (404 not found error occured). when the file exists i get a 500 error too.
I've asked the hosting support without success for the moment..
EDIT :
Strangely, when i write :
RewriteEngine On
RewriteRule (.*) somefile.txt [QSA]
it works on bot, so it seems that the problem comes from executing php files..
EDIT2 :
Here is the real .htaccess file i try to use :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#RewriteCond %{REQUEST_FILENAME} -f
#RewriteRule .? - [L]
#####################
RewriteRule (.*) app/public/$1
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
RewriteRule app/public/(.*) $1
#####################
#RewriteCond %{REQUEST_FILENAME} question2answers/(.+)
#RewriteRule .? - [L]
#####################
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule (.*) index.php [L,QSA]
</IfModule>
You can see that only the public block is not commented but when i go to the website i get this :
The requested URL /cgi-bin/php5.fcgi/index.php/index.php/index.php/index.php/index.php/index.php/index.php/in.....
but i request a file in the public folder (like http://website.com/css/style.css (which is in public/css) it works as expected.
When i'm not commenting the last block, i get a 500 error.
Ok, i found a solution to avoid redirections :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#####################
RewriteCond %{QUERY_STRING} stop
RewriteRule (.*) - [L]
#####################
RewriteCond %{REQUEST_URI} question2answers/(.*)
RewriteRule (.*) $1?htaccess=stop [L,QSA]
#####################
RewriteRule (.*) app/public/$1
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule (.*) $1?htaccess=stop [L]
RewriteRule app/public/(.*) $1
#####################
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php?htaccess=stop [L,QSA]
</IfModule>
I do not understand all but it works.
Try adding in your htaccess file:
Options -Multiviews
It turns off content negotiation which can sometimes screw with how URLs map to files with extensions like php.

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.