Daniel Gabriel
Daniel Gabriel

Reputation: 3985

.htaccess not rewriting the URL correctly for .php files, but OK for .html files

I'm trying to accomplish 2 things in my .htaccess:

  1. Redirect all requests for (in example) www.blanklabs.com/boarddrive/faq, www.blanklabs.com/boarddrive/faq.htm, www.blanklabs.com/boarddrive/faq.html, or www.blanklabs.com/boarddrive/faq.php to www.blanklabs.com/boarddrive/faq.php

  2. The browser's address bar should show just www.blanklabs.com/boarddrive/faq, without the extension.

Here is my current .htaccess:

RewriteEngine On

# -- new
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)\.html$ $1.php [L,QSA]

On the server I have faq.html (for now), but I also tried having both faq.html and faq.php. Eventually it'll just be faq.php.

The .htaccess is clearly incorrect, since if I go to www.blanklabs.com/boarddrive/faq.html I get the correct content (from faq.html), but if I go to www.blanklabs.com/boarddrive/faq.php I get a 500 error. This happens even if I have faq.php on the server.

Any ideas what I'm doing wrong? The no-extensions is secondary, the primary goal is to redirect all requests from html to php files.

Upvotes: 1

Views: 120

Answers (2)

anubhava
anubhava

Reputation: 785286

Place this code in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On

# skip POST requests
RewriteCond %{REQUEST_METHOD} POST
RewriteRule ^ - [L]

RewriteCond %{THE_REQUEST} \s/+(.+?)\.(php|html?)[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)(\.html?)?$ /$1.php [L,QSA]

Upvotes: 1

Anil Maharjan
Anil Maharjan

Reputation: 441

You need to redirect /faq.htm /faq.php

[using redirect directive]

to /faq

now just applying rewrite rule to this later condition [/faq] to

/faq.php

It should work.

Upvotes: 0

Related Questions