2

After upgrading my remote server to Ubuntu 14.04 (Apache 2.4), I am unable to use my previous format where I could request URI's without calling the file extensions kind of like how pages are called in a framework like CodeIgniter.

When I call http://example.com/about I am calling about.php. This worked in Ubuntu 12.04 but now getting Page not found.

This still works on my local machine which is still 12.04 so I do not believe this to be an .htaccess issue nor do I believe it to be a sites-available file issue. Also, this is version controlled using git so everything is pretty much the same except for a couple Vars that call the site name and file paths. --CORRECTION: This is a .conf issue as stated below in my UPDATE.

I have a2enmod (mode_rewrite) on.

But to be complete, I will provide both the htaccess and sites-available files;

/etc/apache2/sites-avaialable/example.net.conf:

<VirtualHost xxx.xxx.xxx.xxx:443>
  SSLEngine On
  SSLCertificateFile /etc/apache2/ssl/domains/example.net/example.pem
  SSLCertificateKeyFile /etc/apache2/ssl/domains/example.net/example.key

  ServerAdmin webmaster@example.net
  ServerName example.net

  DirectoryIndex index.php index.html
  DocumentRoot /var/www/html/example.net/www/

  <Directory />
    Options FollowSymLinks
    AllowOverride None
  </Directory>

  <Directory /var/www/html/example.net/www>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Order allow,deny
    allow from all
  </Directory>

  # Log file locations
  LogLevel warn
  ErrorLog  /var/www/html/example.net/logs/error.log
  CustomLog /var/www/html/example.net/logs/access.log combined
</VirtualHost>
<VirtualHost *:80>
  # Admin email, Server Name (domain name), and any aliases
  ServerAdmin webmaster@example.net
  ServerName example.net
  ServerAlias www.example.net

  # Index file and Document Root (where the public files are located)
  DirectoryIndex index.php index.html
  DocumentRoot /var/www/html/example.net/www/

  <Directory />
    Options FollowSymLinks
    AllowOverride None
 </Directory>

  <Directory /var/www/html/example.net/www>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Order allow,deny
    allow from all
  </Directory>

  # Log file locations
  LogLevel warn
  ErrorLog  /var/www/html/example.net/logs/error.log
  CustomLog /var/www/html/example.net/logs/access.log combined
</VirtualHost>

END example.net.conf

I should note that this is the same in my local machine where the extension-less URI calls succeed.

UPDATE:

I have removed the code to my .htaccess file as I have decided to no longer use it since it is unnecessary and slows down the server.

I currently have this set in my example.net.conf file at the bottom of both (80 and 443) VirtualHost directives:

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php

This works to some extent. The issue I am having is that navigating to document root -> example.com, now just shows an index of all files and directories instead of the content.

earth2jason
  • 171
  • 1
  • 7
  • 'AllowOverride None' means ignore .htaccess files. Try to set 'AllowOverride All'. – user241 Sep 09 '14 at 08:20
  • Thanks for the help user241. Maybe I can now use some of the hacks to make this work now. Although I'm still baffled as to how this is working on my local machine. Also, I've noticed that changes made to htaccess do effect the site so I am actually confused by this. – earth2jason Sep 09 '14 at 23:18
  • As an update, I have decided to remove .htaccess and just use the .conf file in sites-available. I modified AllowOverride back to None. I found some hacks [here](https://stackoverflow.com/questions/4026021/remove-php-extension-with-htaccess) however, these do not work and I suppose it has something to do with upgrading to apache 2.4 as I am also getting DefaultType deprecated when using one of the solutions. – earth2jason Sep 10 '14 at 23:40

2 Answers2

5

The problem lies in the .conf file (or .htaccess if you are using that).

I had the following in my .conf file:

<IfModule mod_rewrite.c>
    Options +FollowSymLinks -MultiViews
    RewriteEngine on

    RewriteCond %{REQUEST_URI} !-d
    RewriteCond %{REQUEST_URI}\.php -f
    RewriteRule ^(.*)$ $1.php [L]
</IfModule>

I finally learned that as of Apache 2.2, you must prepend DOCUMENT_ROOT. So the following now works:

<IfModule mod_rewrite.c>
    Options +FollowSymLinks -MultiViews
    RewriteEngine on

    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f
    RewriteRule ^(.*)$ $1.php [L]
</IfModule>
earth2jason
  • 171
  • 1
  • 7
  • Sitting with Apache 2.4.7 on Ubuntu 14.04, and I cannot get this to work. I get a 404 as soon as I omit the .php extension :( – Robert Dec 29 '15 at 08:57
  • Did you get this figured out. Been a while since I've worked with these kind of Apache issues, but have you studied the Codeigniter URL docs. Tk421 has a link below. Also have your ran a2enmod and turned on your modules. I also noticed that Aache rules are a bit more tricky now. You want to make sure that re-writing correctly. Sorry this couldn't have been more help. – earth2jason Jan 01 '16 at 16:01
  • No, haven't gotten this to work yet. mod_rewrite is enabled. I'll have a deeper look at rewrite rules, as so far, no example that I've "copypasted" seem to work. Thank you anyway. – Robert Jan 02 '16 at 08:32
  • Ok! Got it to work. Just did a simple rule based of if `%{DOCUMENT_ROOT}%{REQUEST_URI}\.php`is a file, and just add `.php` in that case :) – Robert Jan 03 '16 at 09:25
  • Note! I had to add the rewrite directives in my virtual host declaration, I couldn't get it to work on "top level" per your solution. Otherwise the rewrite directives are pretty much exactly the same. – Robert Jan 03 '16 at 09:33
  • Thanks. That's save lot of time. Keep it up. Good bless you. – Manish Feb 13 '20 at 03:38
0

I created a configuration example that is able to execute /testclass/testmethod without the .php extension

For the rewrite rules I followed the guideline at codeigniter

Tk421
  • 220
  • 1
  • 8