SEO friendly URLs

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

By gversion - February 3, 2013

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

By gregThomas - February 4, 2013

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

By gversion - February 4, 2013

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

By gregThomas - February 4, 2013

Hi Greg,

I tried both the links below, but neither is working for me. Is this a server that you have set up from scratch? My guess is you might not have mod rewrite enabled/installed. I'm not sure what the next step should be as the defaults installed and linux variant used is different for every hosting company.  I would contact your hosting company about this issue. 

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com