如何使用.htaccess隐藏.php网址扩展?

时间:2022-07-18 10:42:30

I want something to put in my .htaccess file that will hide the .php file 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/$1 [R,L]

ErrorDocument 404 /error/404.php

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]

4 个解决方案

#1


38  

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]

#2


4  

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.php作为/ file访问

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!

快乐htaccessing!

#3


2  

<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 ^(.+)$ $1\.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错误。删除注释修复它。所以移动评论所以他们按照自己的方式行事,而不是像指示一样。

#4


0  

Try this:

尝试这个:

# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://example.com/folder/$1 [R=301,L]

# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://example.com/folder/$1 [R=301,L]

# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]Try:

Reference:

参考:

How to hide .php extension in .htaccess

如何在.htaccess中隐藏.php扩展名

#1


38  

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]

#2


4  

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.php作为/ file访问

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!

快乐htaccessing!

#3


2  

<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 ^(.+)$ $1\.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错误。删除注释修复它。所以移动评论所以他们按照自己的方式行事,而不是像指示一样。

#4


0  

Try this:

尝试这个:

# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://example.com/folder/$1 [R=301,L]

# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://example.com/folder/$1 [R=301,L]

# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]Try:

Reference:

参考:

How to hide .php extension in .htaccess

如何在.htaccess中隐藏.php扩展名