How to Create .htaccess rewrite rule ignoring authentication requests - php

I am running cpanel with apache 2.2.25 and have the following site custom configuration setup
WSGIScriptAlias /trac /usr/share/trac/cgi-bin/trac.wsgi
<Directory /usr/share/trac>
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
<Location "/trac/login">
AuthType Basic
AuthName "Trac"
AuthUserFile /home/[user]/public_svn/conf/htpasswd
Require valid-user
</Location>
I have tried the following .htaccess configuration, and others, but the authentication prompt is not displaying when I go to www.mydomain.com/trac/login , instead it is redirecting to index.php .
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^svn - [L,NC]
RewriteRule ^trac - [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
Is there a way to create .htaccess rewrite condition to skip authentication requests?

This is a solution that works, but I don't know why. I added the following to the beginning of the index.php file.
<?php
if (substr($_SERVER['REQUEST_URI'],0,4) == '/svn') {
die();
}
if (substr($_SERVER['REQUEST_URI'],0,5) == '/trac') {
die();
}
?>
If anyone knows why this works I'd love to know!

Related

Php front controller works with errors

I'm trying to make simple routing with php using apache. This is my .htaccess file:
`
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
'
This is my index.php :
<?php
echo "request = " . $_SERVER['QUERY_STRING'];
Also i got test.php in directory.As far as i understand it should redirect all requests that is not an existent files ot folders. All works fine for random request and works directly for localhost\test.php. But once i try localhost\test or localhost\test\new It doesnt get redirected to the index.php. The requested URL /test/new was not found on this server. What do i miss?
And there is my apache settings:
<Directory /var/www/public>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>

apache basic auth breaks rewrite in .htaccess

rewriting works fine until we add auth to htaccess. Is auth formatted correctly? Why would rewrite work before adding auth?
Request params at the site controller with no auth:
[id] => 433 [type] => city
Params after auth added and the user authenticated:
[site/query]
Sample query url that works without auth:
http://www.website.com/site/query?type=city&id=433
The .htaccess with basic auth at the bottom:
# any visits not coming from this official URL should be rerouted; AJAX cross-domain, www.website.com is not the same as our.company.com
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.website\.com$ [NC]
RewriteRule ^(.*)$ http://www.website.com/$1 [R=301,L]
# route all requests that are NOT static files, through index.php to make the /nice/urls/ work
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L]
# disable file listings for directories
Options -Indexes
# disable the fetching of .phtm
<Files ~ "\.phtml$">
Order allow,deny
Deny from all
</Files>
# Protect hidden files from being viewed
<Files .*>
Order Deny,Allow
Deny From All
</Files>
# Protect application and system files from being viewed
RewriteRule ^(?: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
AuthType Basic
AuthName "Password required"
AuthUserFile /maps/scorp/main/.htpasswd
Require valid-user
I've got no answer to your nice questions but I hope I found a solution. :)
IMHO in your .htaccess the authorisation will take the advantage. It's a kinda tricky but you can break the priority by enclosing the "Require valid-user" directive into block <Files>. E.g.:
<Files "*.phtml">
Require valid-user
</Files>

Codeiginter : Error in URL

My front url is-
http://localhost/myProject/
admin url is -
http://localhost/myProject/admin
It works in windows but not works on Ubuntu.
It gives error "Not found".
What works in ubuntu -
The front page is working - http://localhost/myProject/
The admin login page is working if I add index.php in url like this -
http://localhost/myProject/index.php/admin
Not other pages are working
My .htaccess file contents-
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|themes)
RewriteRule ^(.*)$ /myProject/index.php/$1 [L]
My Apache's mod-rewrite module is on.
I was also following the similar issue in Centos, and I followed the below tutorial :
How to permit changes in the .htaccess file:
open httpd using ------------------> vi /etc/httpd/conf/httpd.conf
Once inside that file, find the following section, and change the line that says AllowOverride from None to All. The section should now look like this:
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
After you save and exit that file, restart apache. .htacess files will now be available for all of your sites.
service httpd restart
Content of .htaccess file:
<IfModule mod_rewrite.c>
Options +FollowSymLinks -Indexes
RewriteEngine on
# NOTICE: If you get a 404 play with combinations of the following commented out lines
#AllowOverride All
#RewriteBase /
# Restrict your site to only one domain
#RewriteCond %{HTTP_HOST} !^www\.
#RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|fonts|js|images|robots\.txt|css)
<IfModule mod_php5.c>
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_php5.c>
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
</IfModule>
#prevent access to htaccess file
<Files .htaccess>
order allow,deny
deny from all
</Files>
#disable directory browsing
Options All -Indexes
IndexIgnore *

Can't remove index.php, custom route not working

I know this question is more appropriate for Server Fault but unfortunately I was banned for poor quality questions (I was down voted on 2-3 questions I asked.) So the next best place to ask these questions are here.
I have two problems related to CodeIgniter routing.
The first problem is that I can't seem to get rid of index.php in the url. I followed the instructions on how to remove it. I have the following mod rewrite code in my .htaccess file (see below) at the root of my WAMP server (CI is located at the root, not in its own folder). I have uncommented this line in httpd.conf file LoadModule rewrite_module modules/mod_rewrite.so. I deleted index.php from $config['index_page'] = "index.php";. And I restarted all WAMP services.
My second problem is that I have a controller called search and a method called index. I would like to change the resultant URL from http://localhost/index.php/search/index to http://localhost/search/whatever_im_searching_for. I tried the following custom route in my routes.php file but it did not work: $route['search/(.*)'] = "search/$1";
RewriteEngine On
# Put your installation directory here:
# If your URL is www.example.com/, use /
# If your URL is www.example.com/site_folder/, use /site_folder/
RewriteBase /
# Do not enable rewriting for files or directories that exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# For reuests that are not actual files or directories,
# Rewrite to index.php/URL
RewriteRule ^(.*)$ index.php/$1 [PT,L]
I am struggling to understand the code in .htaccess and on how to use CI's custom routing. Any assistance will be greatly appreciated. Thank you in advance.
EDIT 1
Edit your htaccess like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_URI} ^LoginTut.*
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond $1 !^(index\.php|images|table-images|js|robots\.txt|css|captcha)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
To have your searchterms in the url you can look at this:
https://stackoverflow.com/a/12070284/1379394
Second problem:
$route['search/(:any)'] = "search/index/$1";
Check Apache's default config file. On WAMP it's probably in
<WAMPSERVER_HOME>\bin\apache\Apache2.2.xx\conf
If it looks like this:
DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
then change both:
AllowOverride None
to:
AllowOverride All
I had the same problem and I fixed only by write in my .htaccess:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
and it's working perfectly.

Wordpress authentication issue

I am having problem with an htaccess authentification.
I have a wordpress website and would like to restrict access on it. In the root folder, i have added an htaccess and a htpasswd.
Htaccess codes:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
AuthType Basic
AuthName "Protected Area"
AuthUserFile /home/anglolabs/webapps/financial/.htpasswd
Require valid-user
And the htpasswd is as such:
admin:$apr1$wcax9ykl$uH5ktq9NL/9fqtw5lzYLy1
I am being prompted the box but when entering the username/password, I am being redirected to "500-Internal Server Error".
Sounds like the HTTP Auth is working, but you Apache/PHP is busted.
500 means there was an error executing the PHP. Check your server logs, if you have questions post back what the logs show (might need to use Github Gist or Pastbin)

Categories