views:

166

answers:

2

This is my current .htaccess

Options +FollowSymLinks
DirectoryIndex index.php

RewriteEngine On
RewriteBase /product/

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.php$ ./$1/ [R=301,L]

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]

RewriteRule ^contact/?$ ./contact.php [QSA,L]

Everything is working fine now..

  • /product/contact return to /product/contact/
  • /product/contact.php return to /product/contact/

Question..

  • How to make all my .php will be /contact/ /help/ /faq/
  • Now I should to add contact.php help.php faq.php to the htaccess
  • I tried to add RewriteRule ^(.*)/$ $1.php [QSA,L] but it will be return loop.

Let me know how to fix it ;)

A: 

Is it possible that this line is backwards?

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

As I understand it, this rewrites domain.com/product/help/ to domain.com/product/help.php. Try

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

This should rewrite domain.com/product/help.php to domain.com/product/help/ and domain.com/foobar/baz.php to domain.com/foobar/baz/ etc.

immibis
not working :) thanks your input
bob
A: 

Sadly to do this in your rewrite statements will make an infinite loop, since Apache will try to rewrite help/ to help.php, and then help.php to help/ etc etc.

If it's really necessary, do it in your application (example in PHP):

<?php 
// The (\?.*)? so it "ignores" query strings
if (preg_match('/\.php(\?.*)?$/', $_SERVER['REQUEST_URI'])) {
  header('Location: ' . str_replace('.php', '/', $_SERVER['REQUEST_URI']));
  exit;
}
Tobias Baaz
yeah.. this way can be done
bob
Escape the dot in the regular expression. Otherwise you will get an infinite loop if the URI path just ends in a `php` like in `foo-php` or `bar/php`.
Gumbo
Nice catch Gumbo, fixed the answer. Thanks!
Tobias Baaz