-3

I want to redirect using .htaccess abc.com/test.php to abc.com/test. but it will execute/display the content of test.php only.

how should I do this? any suggestions?

Jaron S.
  • 81
  • 3
  • 10
  • Do you not mean the other way around? As in if someone put in their browser http://abc.com/test you'd like to show them the test.php contents? – Rob Forrest Jun 14 '12 at 12:30
  • possible duplicate of [How can I use .htaccess to hide .php URL extensions?](http://stackoverflow.com/questions/10028025/how-can-i-use-htaccess-to-hide-php-url-extensions) – Quentin Jun 14 '12 at 12:31
  • Can you provide us with an example of your current attempt, so we can see what you are actually trying to do? I think @RobForrest is right, but if we see the code we can tell for certain. – Rem.co Jun 14 '12 at 12:31
  • yes. I want the same thing. if user directly write `abc.com/test` then its okay. it should display `test.php` files contents only. – Jaron S. Jun 14 '12 at 12:31

2 Answers2

2

Try that

RewriteRule ^test$ test.php [L]

[update]

A complete redirect

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME}/ -d
RewriteCond %{SCRIPT_FILENAME}.php !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.php$ /$1 [R=301,L]
RewriteCond %{SCRIPT_FILENAME}.php -f
RewriteRule [^/]$ %{REQUEST_URI}.php [QSA,L] 
Sena
  • 916
  • 5
  • 13
  • I Would add the `QSA` flag as well as David has done in his answer, so potential query string will be appended too. – Havelock Jun 14 '12 at 12:34
  • `abc.com/test` will display content of `abc.com/test.php` but `abc.com/test.php` should redirect to `abc.com/test` – Jaron S. Jun 14 '12 at 12:35
  • @user985361 updated my answer to a better code – Sena Jun 14 '12 at 12:53
  • @sena it is working for all the files.. what if I want to implement it for specific files onle.. like test.php – Jaron S. Jun 14 '12 at 13:55
1

This is accomplished using Rewrite Rules, rather than redirects. Something like this:

RewriteEngine On
RewriteRule ^/test.php$ /test [L,QSA]
Ashley Strout
  • 6,107
  • 5
  • 25
  • 45