Server Fault Asked by Wobbo on February 28, 2021
For example: I create a Link to PHP script file to file.php <?php echo 'exaple'; ?>
. When I open file online I get the PHP script not exaple. If I open file.php I get exaple.
I have tried if .htaccess works. I made file.html <?php echo 'exaple'; ?>
. And .htaccess:
<IfModule mod_rewrite.c>
AddType application/x-httpd-php .html
</IfModule>
When I open file.html I get exaple.
I have tried the same way to get file working:
AddType application/x-httpd-php
AddType application/x-httpd-php *
AddType application/x-httpd-php file
AddType application/x-httpd-php ^ (.*)$
AddType application/x-httpd-php ^ (.*)
AddType application/x-httpd-php ^ (.)
AddType application/x-httpd-php ^.
No succes. How can I get this runnig?
You are barking up the wrong tree with AddType
.
By the sounds of it you are trying to implement extensionless URLs.
You can do this by simply enabling MultiViews. For example:
Options +MultiViews
Now, when you request /file
it will serve /file.php
(or /file.html
) if it exists. Like "magic".
Reference: https://httpd.apache.org/docs/2.4/content-negotiation.html
OR, use mod_rewrite to internally rewrite the URL. For example:
Options +FollowSymLinks -MultiViews
RewriteEngine On
# Internally rewrite "/file" to "/file.php"
RewriteRule ^file$ file.php [L]
The above rewrites just the specific request URL /file
to /file.php
. It does not first check whether /file.php
exists, so in this respect it is unconditional.
However, exactly how you implement this will depend on your file structure and possibly your existing directives. More generally, to rewrite any request for an extensionless file using mod_rewrite, you would change the above RewriteRule
to something like:
# Internally rewrite "/<something>" to "/<something>.php"
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !.w{2,4}$ %{REQUEST_URI}.php [L]
The above rewrites any request that does not have, what looks like, a file extension and does not map to a physical directory by appending a .php
extension.
Reference: https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html
However, MultiViews and mod_rewrite do not necessarily play well together, so if you are using mod_rewrite you should probably disable MultiViews
Answered by MrWhite on February 28, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP