SEO friendly URLs

5 posts by 2 authors in: Forums > CMS Builder
Last Post: February 4, 2013   (RSS)

Hello,

I have managed to get the SEO friendly URLs working so that question marks are replaced with forward slashes, however I can't seem to get the rewrite rule to remove the ".php" extension. I am using the following in my httpd file under my <VirtualHost> setting:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME}.php -f

RewriteRule ^(.+)$ $1.php [L,QSA]

When I remove the ".php" from the file name I receive a 404 error message.

Could someone please give me a hand getting this to work?

Thank you,

Greg

Hi Greg,

I've done some quick local testing, and this htaccess rule worked for me:

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

For this to work your Apache server will need to have the rewrite engine enabled, but most hosting companies have this module switched on by default.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

Hi Greg,

Thanks for your help. I am running a Cloud Server with Rackspace and I have added the code to my virtualhost setting in /etc/conf/httpd which now looks as follows:



  # Admin email, Server Name (domain name) and any aliases
  ServerAdmin admin@mydomain.com
  ServerName  demo.mydomain.com
  ServerAlias demo.mydomain.com
 
  # Index file and Document Root (where the public files are located)
  DirectoryIndex index.php
  DocumentRoot /home/demo/public_html

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

However, this is not working... I still receive a 404 error message. Any ideas what else I could try?

This URL works: http://demo.mydomain.com/category.php/1/
This URL does not work: http://demo.mydomain.com/category/1/

Thanks,

Greg

Hi Greg,

Yes, this is a server I setup myself... mod_rewrite is installed because it's working on other websites I am hosting. Maybe it's not enabled for this particular account.

I will contact my hosting company as you suggested and see if they can help.

Thanks for looking into this for me.

Regards,

Greg