使用 .htaccess 文件修改 url [英] Modify url with .htaccess file

查看:68
本文介绍了使用 .htaccess 文件修改 url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的网址中删除 .php、.html 等扩展名,这就是我使用的:

I would like to delete extensions like .php, .html, etc from my url and this is what I use:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]

它有效,但我不知道这是否是最好的方法,我也不明白它是如何工作的,有人可以解释一下吗?

It works but I don't know if this is the best way to do this and I also don't understand how it works, could someone explain it?

我想意识到的第二件事是通过执行以下操作使 URL 更美观:

The second thing I would like to realise is to make the URL more beautiful by doing something like this:

这是我的网址:http://domain.com/portfolio/project.php?id=1这是我想看到的:http://domain.com/portfolio/project/1

this is my URL: http://domain.com/portfolio/project.php?id=1 this is what I would like to see: http://domain.com/portfolio/project/1

提前致谢

推荐答案

您可以在站点根目录 .htaccess 中使用这些规则:

You can use these rules in site root .htaccess:

RewriteEngine On

# rewrite /portfolio/project/25 to /portfolio/project.php?id=25
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(portfolio/[\w-]+)/([\w-]+)/?$ $1.php?id=$2 [L,QSA,NC]

# rewrite /portfolio/project to /portfolio/project.php
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

# rewrite /portfolio/file to /portfolio/file.html
RewriteCond %{DOCUMENT_ROOT}/$1\.html -f
RewriteRule ^(.+?)/?$ $1.html [L]

这篇关于使用 .htaccess 文件修改 url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆