Apache2 setting up mod_rewrite and restricting access without .htaccess file - php

I am following this tutorial:
http://www.phpro.org/tutorials/Model-View-Controller-MVC.html
The tutorial states you should use an htaccess file. However, the Apache2 documentation advises you to enter your .htaccess rules into the standard configuration files for better performance.
I'm working in /etc/apache2/sites-available/default.
This is what I have so far:
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order Deny,Allow
Deny from all
</Directory>
<Directory /var/www/>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?rt=$1 [L,QSA]
</Directory>
<Location /index.php>
Order Allow,Deny
Allow from All
</Location>
With these rules, index.php is accessible, but index.php?rt=blog still works, and index/blog or /blog do not. What am I doing wrong?

but index.php?rt=blog still works
You don't have any rules that make it so that doesn't work
and index/blog or /blog do not
You don't have any rules that make URLs that look like that work, though "/blog" should be rewriting to /index.php?rt=/blog.
Try something like:
<Directory /var/www/>
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /+index\.php?rt=([^&\ ]+)
RewriteRule ^ /%1? [L,R]
RewriteRule ^/index/(.*)$ /$1 [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/(.*)$ /index.php?rt=$1 [L,QSA]
</Directory>

Related

.htaccess not working - CodeIgniter

I've tried to host a CodeIgniter website in Ubuntu server.
All other websites are working fine without any issues (the sever contains WordPress and Laravel applications). But this particular CodeIngniter website is not taking .htaccess file. I've spend a day to figure out the issue, but no luck.
Here is the details.
Website url structure: http://example.com/website_name
.htaccess file
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Virtual host entry
<VirtualHost *:80>
ServerName example.com
ServerAlias example.com
DocumentRoot "/var/www/example.com"
<Directory "/var/wwww/example.com">
Options Indexes FollowSymLinks MultiViews
AllowOverride ALL
Order allow,deny
allow from all
</Directory>
</VirtualHost>
CodeIgniter config file
$config['base_url'] = 'http://example.com/website_name/';
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';
And when I'm trying to access the website the output is as follows,
Not Found
The requested URL /website_name/test was not found on this server.
But if I add index.php, the the website is working without any issues. I've tried lot methods and it doesn't worked.
try this .htaccess
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options -Indexes
RewriteEngine On
RewriteBase /website_name/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ index.php [L]
</IfModule>
Add to your .htaccess
RewriteBase /website_name/
after your RewriteEngine on
I have a feeling MultiViews is probably causing some issues.
Change this in your VHOST
Options Indexes FollowSymLinks MultiViews
to this
Options Indexes FollowSymLinks
And also probably add a rewritebase to .htaccess Rewrite
RewriteBase /website_name/
Restart apache for config changes
Try changing your .htaccess like this.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/system.*
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?/$1 [L]
Or this
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
Not the ultimate answer but a good way to test if .htaccess is working at any level.
Create this .htaccess and put in root directory
RewriteEngine On
Options +FollowSymLinks
RewriteRule ^test\.html http://www.google.com/? [R=301,L]
Then direct a browser to
http://example.com/test.html
If you end up on Google then you know .htaccess is working.
A useful way to debug .htaccess is to use its logging function.
Add this to your vhost's configuration:
RewriteLog "/tmp/rewrite.log"
RewriteLogLevel 9
You'll probably need to restart apache to get logging started.
Finally I've figured out the issue by spending an entire day. Here is the solution.
Updated virtual host entry.
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/example.com/
# Additional section added to get the htaccess working in sub folder.
Alias /website_name/ /var/www/example.com/website_name/
<Directory /var/www/example.com/website_name/>
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
allow from all
</Directory>
</VirtualHost>

.htaccess not working with Laravel

I'm using Laravel 5.1 with a PuPHPet VM using Apache (so not Homestead).
I've got the default .htaccess in my public dir;
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
I've been in to the apache2.conf;
AccessFileName .htaccess
<FilesMatch "^\.ht">
Require all granted
</FilesMatch>
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
And I've restarted apache2;
sudo service apache2 restart
But I still get a not found on;
http://192.168.56.131/auth/register
But not on;
http://192.168.56.131/public/auth/register
So I'm not sure what to try now. Part of it must work because the index.php is no longer required
* UPDATE *
This is working;
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
But this does not;
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
It just doesn't like the whole "public" thing.
This is an updated partial of my apache2.conf file;
AccessFileName .htaccess
<FilesMatch "^\.ht">
Require all denied
</FilesMatch>
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
So frustrating! I don't understand what I am missing here.
Your Apache config should go within the vhost config file, not htaccess.
PuPHPet uses php-fpm, mod_php support was dropped several months ago.
Try
RewriteEngine On
RewriteRule ^(.*)$ /public/$1 [L]

htaccess rewrite rule is not working

All,
I'm trying to redirect the hits via htaccess redirect scripts.
Below is my htaccess scripts.
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^post/(.*)/$ post.php?tag=$1 [L]
RewriteRule ^search/(.*)$ search.php?tag=$1 [L]
Issue : Index and search page are getting redirected. Post page is not getting redirected.
Please help on how to fix it
Try this in your root .htaccess:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^post/(.+)$ post.php?tag=$1 [L,NC,QSA]
RewriteRule ^search/(.+)$ search.php?tag=$1 [L,NC,QSA]
Apache out of the box doesn't allow use of htaccess files. If you haven't edited your /etc/apache2/sites-available/default file, you'll need to do so. Look for this chunk of code:
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
# Uncomment this directive is you want to see apache2's
# default start page (in /apache2-default) when you go to /
#RedirectMatch ^/$ /apache2-default/
</Directory>
And change AllowOverride None to AllowOverride All. After you've changed that, you'll need to restart Apache for the changes to take place (service apache2 restart).
Caveat
Most of what you'd want to do via htaccess files can be done more securely via the Apache configuration files (hence the htaccess files are ignored by default). See this post for details.

Framework URL rewriting

When I load http://localhost/test, I get redirected to http://localhost/public/?_url=/test
Here is my Apache config. I also tested to make sure rewrite_module is loaded.
DocumentRoot "/var/www/myapp/"
<Directory "/var/www/myapp/">
Options -Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>
I created the project using Phalcon Dev Tools phalcon project myapp. Here are the two .htaccess files it created automatically.
cat /var/www/myapp/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
</IfModule>
cat /var/www/myapp/public/.htaccess
AddDefaultCharset UTF-8
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
</IfModule>
Using Phalcon 1.2.6, Apache/2.2.24
Firstly your apache config should be something like
DocumentRoot "/var/www/myapp/public/"
<Directory "/var/www/myapp/public/">
Options -Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>
Secondly there should be a .htaccess file in your public directory like this one :-
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
</IfModule>
I hope this resolves this issue.

Rewrite rule for subfolder

I have the file structure
index.php
.htaccess
news/index.php
news/.htaccess
First .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/news/
RewriteRule . /index.php [L]
Second (news/.htaccess)
RewriteEngine On
RewriteRule . /index.php
Request http://test.t/news/news/61 the handles first index.php but I need to do it the second
I tried a few more options for the first .htaccess, but it did not succeed
Check your Apache config file (httpd.conf) and make sure the directory you are using for your site includes the AllowOverride option.
Example:
<Directory "/Applications/MAMP/htdocs">
Options All
AllowOverride All
Order allow,deny
Allow from all
</Directory>

Categories