Title: Redirect request and hide .php extension from the request
Tags: apache;.htaccess;mod-rewrite;url-rewriting
Question: We have started to change our website, and we use the old and the new site together. If something not exist on the new site we just redirect them back to the old one. (This is a transparent process for the user.)
This is how .htaccess rules looks like now:
RewriteEngine On
RewriteBase /
RewriteCond %{http_host} ^domain.hu [NC]
RewriteCond %{http_host} !^domain.hu.data18.websupport.sk$ [NC]
RewriteRule ^(.*)$ http://www.domain.hu/$1 [R=301,NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/static-old%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/static-old%{REQUEST_URI} -d
RewriteRule ^(.*)$ /static-old/$1 [QSA]
RewriteRule ^(.*)\.html$ $1.php [L,R=301,QSA]
So if we don't find the the file on the main directory, but we find it in the old one we serve the old one instead of a 404. And if the requested file has html extension then we rewrite it internally to php, because we changed this a while ago.
I want to hide the php extension (and so html will be hidden too) so if a requested uri is /something.php or /something.html then only show /something to the user. But I if I edit .htacces like this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/static-old-from-seocms%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/static-old-from-seocms%{REQUEST_URI} -d
RewriteCond %{REQUEST_URI} ^(.*?)\.php[\s?] [NC]
RewriteRule ^/%1/ [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/static-old-from-seocms%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/static-old-from-seocms%{REQUEST_URI} -d
%{REQUEST_URI} !\.(.*)$
RewriteRule ^(.*)$ $1.php [L,R=301,QSA]
RewriteRule ^(.*)$ /static-old-from-seocms/$1 [QSA,L]
I will get an 500 internal server error, the .htaccess parsing  fail.
I think this script will check if the requested file has php extension and does not exist in the main directory then redirect it to the same address without the php extension, after that the script will check if the requested uri is not exist in the main directory and does not have an extension then write it a php extension to it internally.

Here is the accepted answer: Try this .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{http_host} ^domain\.hu [NC]
RewriteCond %{http_host} !^domain\.hu\.data18\.websupport\.sk$ [NC]
RewriteRule ^(.*)$ http://www.domain.hu/$1 [R=301,L]
RewriteCond %{THE_REQUEST} \s/+(.+?)\.(php|html)[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)/?$ /$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/static-old%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/static-old%{REQUEST_URI} -d
RewriteRule ^(.+)$ /static-old/$1 [L]
Comment for this answer: Actually when tried that URL it got redirected to non-php URL `http://www.unit.hu/ceginformaciok-szoftverfejlesztes` for me and loaded the content.
Comment for this answer: You should use: `ErrorDocument 404 /static-old/errors/404.php` and make sure this 404.php exists.
Comment for this answer: If you comment out `ErrorDocument 404` line then try to load `http://www.unit.hu/static-old/php/mailtous` what exact do you get in your browser?
Comment for this answer: This remove the .php extension, but give me a 404 error. So if I visit www.mydomain.com/page.php the address bar show correctly the www.mydomain.com/page url but I get an 404 from the server.
Comment for this answer: Thats give me now an 500 Server error every page where the rewrite needed
Comment for this answer: Its working now for all url except one: visiting http://www.unit.hu/ceginformaciok-szoftverfejlesztes.php give me a 404 error.
Comment for this answer: I added RewriteCond %{REQUEST_URI} !^/static-old-from-seocms/php/mailtous\.php to the last the block. Is this the right way to do it?
Comment for this answer: The requested URL /static-old/php/mailtous was not found on this server.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2014:00:01/14/14', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<apache><.htaccess><mod-rewrite><url-rewriting>', 'Title': 'Redirect request and hide .php extension from the request', 'ViewCount': 204, 'id': 21120052, 'user_AboutMe': "<p>Look into problems, you'll find solutions. Look into solutions, you'll find problems.</p>\n", 'user_AccountId': 2298911.0, 'user_DisplayName': 'NoNameProvided', 'user_Reputation': 8142.0, 'user_Views': 732.0, 'user_id': 2019689.0}}