php - 在 php 中如何从 URL 中删除查询字符串以及访问页面的文件名?
问题描述
我有一个网址https://testing.in/show_tmp?tmp=abcval
。我想要一个干净的 URL,就像https://testing.in/abcval
不破坏网站的功能一样。我的网站是用 Core PHP 构建的。我知道我们可以使用 htacess 文件来实现这一点,但我不知道该怎么做,请帮帮我。
htaccess 文件代码
# DO NOT REMOVE THIS LINE AND THE LINES BELOW SSL_REDIRECT:connectwithme.in
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^connectwithme.in$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# DO NOT REMOVE THIS LINE AND THE LINES ABOVE SSL_REDIRECT:connectwithme.in
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^.]+)$ $1.php [NC,L]
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/?show_tmp$ [NC]
RewriteRule /?(.+) show_tmp?tmp=$1 [NC,L,QSA]
解决方案
我们添加了一个关于当前请求 URI 不应包含的重写条件,show_tmp
以避免重定向问题。稍后,我们只需捕获请求 URI 并重定向到show_tmp
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/?show_tmp$ [NC]
RewriteRule /?(.+) show_tmp?tmp=$1 [NC,L,QSA,P]
演示: https ://htaccess.madewithlove.be?share=0ddbbf44-57e1-5c9b-9e83-500961e8050d
协助 Anydesk 访问后,您的最终 .htaccess 应如下所示:
# DO NOT REMOVE THIS LINE AND THE LINES BELOW SSL_REDIRECT:connectwithme.in
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^connectwithme.in$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# DO NOT REMOVE THIS LINE AND THE LINES ABOVE SSL_REDIRECT:connectwithme.in
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_URI} !^/?assets/.+$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php !-f #add this for any kind of file extension. If you want to display .html, then you will have to add .html also
RewriteCond %{REQUEST_URI} !^/?assets/.+$ [NC]
RewriteCond %{REQUEST_URI} !^/?show_tmp$ [NC]
RewriteRule (.*) https://%{HTTP_HOST}/show_tmp?tmp=$1 [NC,L,QSA,P]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^.]+)$ $1.php [NC,L]
推荐阅读
- xcode - MacOS:是否可以在操作系统中禁用外部键盘输入?
- ruby-on-rails - Rails 测试无法运行
- ajax - 在 Epic 中测试 Post 请求
- javascript - NgClass 问题 Angular 和 D3
- android - 如何避免在任务完成之前导航到下一个?
- php - 如何在 php 中使用 PDFTK 背景?
- node.js - 将graphql apollo服务器部署到heroku
- typescript - 如何在 Typescript 中将对象与接口进行比较以查看对象是否具有正确的变量类型?
- javascript - 在我的颤动应用程序中输入时无法使键盘向上推动屏幕
- java - 如何通过 Android 的改造在相同的 KEY 中获取 Json 对象和 Json 数组