I have a Kohana 3.3 application, and I have removed /index.php/ from the URI with some rules in an .htaccess file. Everything is working fine, until I come to use Basic Authentication. I can’t access Basic Auth from $_SERVER because the site is served with php-cgi, which is explained here.
The solution in the previous link works, though it adds /index.php/ back into the URI. I can’t seem to manage to add the new rewrite rule without the server throwing a 500.
Here are my current rewrite rules:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
# Options +SymLinksIfOwnerMatch
Options +FollowSymlinks
RewriteEngine On
# RewriteBase /
</IfModule>
# Protect application and system files from being viewed
RewriteRule ^(?:application|modules|system)\b.* index.php/$0 [L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
#RewriteRule .* app/index.php/$0 [PT]
RewriteRule ^(.*)$ index.php/$0 [PT,L]
RewriteRule ^$ index.php/$0 [PT,L]
And here’s what I need to add:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP:Authorization} ^Basic.*
RewriteRule (.*) index.php?Authorization=%{HTTP:Authorization} [QSA,L]
</IfModule>
How can I make this work? And is there a relatively easy way to learn how mod_rewrite works?
Try this code in your DocumentRoot/.htaccess:
DirectoryIndex index.php
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /api/
# Protect application and system files from being viewed
RewriteRule ^(?:application|modules|system)\b /index.php/$0 [L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$0 [L]
RewriteCond %{HTTP:Authorization} ^Basic.*
RewriteRule ^ index.php?Authorization=%{HTTP:Authorization} [L]
Related
I have created Laravel CRUD API and it works fine in localhost(without .htaccess file), but it fails on the live server. When I send a request it returns 404 not found error, or downloads a file depending on .htaccess content. How should I change .htaccsess file for makeing code to work.
this is my .htaccess file which returns not found error.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
There is an 'official' htaccess that should provide you with the basic code to get this working.
See https://github.com/laravel/laravel/blob/master/public/.htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Then, simply make sure that the root points to yourproject/public so it can boot only from yourproject/public/index.php.
I now have a Laravel installation with the default folder structure.
In my root folder I have a .htaccess file like this one.
Options +FollowSymLinks
<IfModule mod_rewrite.c>
# RewriteEngine On
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteRule ^ index.php [L]
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
I have to install in a subfolder WordPress.
So I would like to access to my WP installation using the URL http://www.example.com/ and to Laravel with http://www.example.com/admin.
The thing I know it's that is an .htaccess trick, but, even if I tried many kind of rewrites, I'm not still able to achieve the desired result.
So, in your root .htaccess you should use the following:
RewriteEngine On
# Route any request begining with 'admin/' to laravel
RewriteRule ^admin/(.*)$ public/$1 [L]
# Anything else goes to Wordpress
RewriteRule ^(.*)$ wp/$1
And in your Laravel .htaccess add:
RewriteBase /admin
If you can't live with the trailing slash being mandatory you can try:
RewriteRule ^admin(/(.*))?$ public/$2 [L]
Try this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /your-subdirectory/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /your-subdirectory/index.php [L]
</IfModule>
# END WordPress
I have a problem when uploading a new laravel 5.5 project. When I alter the htaccess in the public_html folder on my server it keeps redirecting. However this problem wasnt there on laravel 5.0. Here's my htaccess from the public_html folder:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
And here's the htaccess in the public folder:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Any suggestions on how to solve this problem?
Beside the fact that it is really not good idea to put a Laravel project in your Document Root, you have a problem with your htaccess file in your Root directory.
Additional I would at least protect all files in your root folder from direct access:
<IfModule mod_rewrite.c>
RewriteEngine On
# Fake 404 for all files in /
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^/?[^/]+$ - [R=404,L]
# Protect rewrite loop: exclude /public
RewriteCond %{REQUEST_URI} !^/?public(/.*)?$
RewriteRule ^(.*)$ /public/$1 [L]
</IfModule>
Am build a web app with angularjs and my public root looks like this
-assets
-snapshots
-snapshots/article/
-snapshots/sections/
-snaphosts/index.html
.htaccess
and am using phantomjs to generate snapshots from siteamp, my htaccess look this like this
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# angular seo
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=/?(.*)$
RewriteRule ^(.*)$ /snapshots/%1 [NC,L]
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
when i try to test the snapshots using the url http://xyx.com/?_escaped_fragment_=/section/news i get http://xyx.com/?_escaped_fragment_=/section/news#!/ instead of http://xyx.com/snapshots/section/news.html as expected.
I figure my .htaccess config could be wrong, am not really good at htaccess configs.
Any help will be appreciated.
I am trying to make SSL and Redirection work perfectly with my web application. I wish to achieve that always https://www.mydomain.com/ should be loaded in the browser - although, if he types a subdomain - it should be redirected to https://subdomain.mydomain.com/ instead.
I mean to say, everything should be SSL - here is what I am doing currently
Options -Indexes
Options +FollowSymLinks
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www) [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !index.php
RewriteRule (.*)\.php$ index.php/$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [QSA,L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
Also, I wish to improve this .htaccess file so to introduce more security measures plus also allow only js, css and images files to be accessible by everyone - rest everything hidden or redirected to a 404 page.
Please guide, I would be greatly thankful!
These rewrite conditions may help you, I use this to make my CodeIgniter go to HTTPS:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L]