如何使用 .htaccess 隐藏 .php URL 扩展名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10028025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I use .htaccess to hide .php URL extensions?
提问by Markum
I want something to put in my .htaccessfile that will hide the .phpfile extension for my php files, so going to www.example.com/dir/somepage would show them www.example.com/dir/somepage.php.
我想在我的.htaccess文件中放一些东西来隐藏.php我的 php 文件的文件扩展名,所以转到 www.example.com/dir/somepage 会显示他们 www.example.com/dir/somepage.php。
Is there any working solution for this? My site uses HTTPS, if that matters at all.
有什么可行的解决方案吗?我的网站使用 HTTPS,如果这很重要的话。
This is my .htaccess at the moment:
这是我目前的 .htaccess:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/ [R,L]
ErrorDocument 404 /error/404.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) \.php [L]
回答by anubhava
Use this code in your .htaccess under DOCUMENT_ROOT:
在 DOCUMENT_ROOT 下的 .htaccess 中使用此代码:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f [NC]
RewriteRule ^ %{REQUEST_URI}.php [L]
回答by starkeen
Remove php extension
删除php扩展
You can use the code in /.htaccess :
您可以使用 /.htaccess 中的代码:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_FILENAME}.php [NC,L]
With the code above, you will be able to access /file.php as /file
使用上面的代码,您将能够以 /file 的形式访问 /file.php
Remove html extension
删除 html 扩展
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_FILENAME}.html [NC,L]
Note : 'RewriteEngine on' directive should be used once at top per htaccess, so if you want to combine these 2 rules just remove the line from second rule. You can also remove any other extensions of your choice, just replace the .php with them on the code.
注意:'RewriteEngine on' 指令应该在每个 htaccess 的顶部使用一次,所以如果你想组合这两条规则,只需从第二条规则中删除该行。您还可以删除您选择的任何其他扩展名,只需在代码中用它们替换 .php 即可。
Happy htaccessing!
访问愉快!
回答by TRiG
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f # if the requested URL is not a file that exists
RewriteCond %{REQUEST_FILENAME} !-d # and it isn't a directory that exists either
RewriteCond %{REQUEST_FILENAME}\.php -f # but when you put ".php" on the end it is a file that exists
RewriteRule ^(.+)$ \.php [QSA] # then serve that file (maintaining the query string)
</IfModule>
Oddly, this code gave me HTTP 500 errors when tried in XAMPP in Windows. Removing the comments fixed it. So did moving the comments so they were on their own lines, instead of on the same lines as instructions.
奇怪的是,当在 Windows 中的 XAMPP 中尝试时,这段代码给了我 HTTP 500 错误。删除评论修复了它。移动评论也是如此,因此它们在自己的行上,而不是与说明在同一行上。
回答by Wes Crow
Try this:
尝试这个:
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://example.com/folder/ [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://example.com/folder/ [R=301,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ .php [L]Try:
Reference:
参考:

