我想把一些东西放在我的.htaccess
文件中,这会隐藏.php
文件的.php
文件扩展名,所以去www.example.com/dir/somepage会显示它们www.example.com/dir/spagepage.php。
有没有任何工作解决scheme? 我的网站使用HTTPS,如果有关系的话。
这是我目前的.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]
在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} ^[AZ]{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]
删除PHP扩展
您可以使用/.htaccess中的代码:
RewriteEngine on RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^ %{REQUEST_FILENAME}.php [NC,L]
用上面的代码,你将能够以/ file的形式访问/file.php
删除html扩展名
RewriteEngine on RewriteCond %{REQUEST_FILENAME}.html -f RewriteRule ^ %{REQUEST_FILENAME}.html [NC,L]
注意:''RewriteEngine on'指令应该在每个htaccess的顶部使用一次,所以如果你想结合这两个规则,只需从第二条规则中删除该行。 您也可以删除您选择的任何其他扩展名,只需将代码中的.php替换为它们即可。
快乐htaccessing!
<Ifmodulee 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) </Ifmodulee>
奇怪的是,这个代码在Windows中的XAMPP中尝试时给了我HTTP 500错误。 删除评论固定它。 所以移动的意见,所以他们是在自己的路线,而不是在指令相同的路线。
尝试这个:
# 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:
参考:
如何在.htaccess中隐藏.php扩展名